0
const books = [
            {
                "name": "John Adventure",
                "year": 2021,
                "author": "John doe",
            },
            {
                "name": "Marry Adventure",
                "year": 2021,
                "author": "Marry doe",
            },
            {
                "name": "Steven Adventure",
                "year": 2021,
                "author": "Steven doe",
            }
        ];

result

{
    "status": "success",
    "data": {
        "books": [
            {
                "name": "John Adventure",
            },
            {
                "name": "Marry Adventure",
            },
            {
                "name": "Steven Adventure",
            }
        ]
    }
}

how to display certain properties in objects, as above? javascript, how to show only name property object in array like that helppp

1

2 Answers 2

1
const data = {
    "status": "success",
    "data": {
        "books": books.map( book => ({"name": book.name)) )
    }
}

the variable data would contain the object you wanted

Sign up to request clarification or add additional context in comments.

1 Comment

books.map(book => ({"name": book.name}), thankss sir
0

use the map operator to iterate each element from books array

const books = [
            {
                "name": "John Adventure",
                "year": 2021,
                "author": "John doe",
            },
            {
                "name": "Marry Adventure",
                "year": 2021,
                "author": "Marry doe",
            },
            {
                "name": "Steven Adventure",
                "year": 2021,
                "author": "Steven doe",
            }
        ];
        
let result = {
  status: "success",
  data: {}
};
result.data.books =  books.map(item => { return {"name":item.name}}); 
console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.