Reputation: 861
Suppose I have array of object as:
const bookDetails = [
{
"bookId": "1235",
"emailId": "[email protected]",
"bookIssue": [{"Book not properly aligned": true, "some issue1": true}]
},
{
"bookId": "1235",
"emailId": "[email protected]",
"bookIssue": [{"some issues with book": true, "some issue2": true}]
}]
I want the O/P as:
[
{"bookId": "1235", "emailId": "[email protected]", "bookIssue": "Book not properly aligned,some issue1"},
{"bookId": "1235", "emailId": "[email protected]", "bookIssue": "some issues with book,some issue2"}
]
For this I tried,
bookDetails.map((i) => i.bookIssue = Object.keys(i.bookIssue[0]).join(","))
It gives the O/p as required but it starts giving value as,
[{"bookId":"1235","emailId":"[email protected]","bookIssue":"0"},
{"bookId":"1235","emailId":"[email protected]","bookIssue":"0"}]
What could be the issue, is there any other way to achieve this?
Upvotes: 0
Views: 61
Reputation: 71
Just update your Map function with forEach and see console.log(bookDetails). You were checking map output and not checking bookDetails. That's why you were confused. also in your code check console.log(bookDetails). it will work as expected bookDetails.forEach((i) => i.bookIssue = Object.keys(i.bookIssue[0]).join(","));
Upvotes: 1
Reputation: 24234
Your code works too, because you're changing the objects bookIssue
(same reference as in bookDetails
). Here's another way of mapping the attribute bookIssue
to the value you want by returning the new object in the map.
const bookDetails = [
{
"bookId": "1235",
"emailId": "[email protected]",
"bookIssue": [{"Book not properly aligned": true, "some issue1": true}]
},
{
"bookId": "1235",
"emailId": "[email protected]",
"bookIssue": [{"some issues with book": true, "some issue2": true}]
}];
const output = bookDetails.map(book => {
return {...book, bookIssue: Object.keys(book.bookIssue[0]).join(',')}
});
console.log(output);
Upvotes: 1
Reputation: 1074385
See my comment and georg's, your code works just fine (other than using map
incorrectly) provided you want to modify the objects in place.
If you want to create new objects in a new array (e.g., using map
correctly), you'd do what you're doing to get the keys but create a new object with the result, like this:
const result = bookDetails.map(entry => {
// Grab the keys from the first entry and join them
const bookIssue = Object.keys(entry.bookIssue[0]).join(",");
// Build the new object to return
return {...entry, bookIssue};
});
Live Example:
const bookDetails = [
{"bookId":"1235","emailId":"[email protected]","bookIssue":[{"Book not properly aligned": true,"some issue1":true}]},
{"bookId":"1235","emailId":"[email protected]","bookIssue":[{"some issues with book": true, "some issue2":true }]}
];
const result = bookDetails.map(entry => {
// Grab the keys from the first entry and join them
const bookIssue = Object.keys(entry.bookIssue[0]).join(",");
// Build the return object
return {...entry, bookIssue};
});
console.log(result);
If bookIssue
could have more than one entry (why is it an array if it can't?) and you wanted all of the entries in bookIssue
joined together, you could use map
on bookIssue
getting all of the keys from its objects and joining them, then join the resulting array:
const result = bookDetails.map(entry => {
const bookIssue = entry.bookIssue
.map(entry => Object.keys(entry).join(","))
.join(",");
// Build the return object
return {...entry, bookIssue};
});
Live Example:
const bookDetails = [
{"bookId":"1235","emailId":"[email protected]","bookIssue":[
{"Book not properly aligned": true,"some issue1":true},
{"another issue": true,"yet another issue":true}
]},
{"bookId":"1235","emailId":"[email protected]","bookIssue":[{"some issues with book": true, "some issue2":true }]}
];
const result = bookDetails.map(entry => {
const bookIssue = entry.bookIssue
.map(entry => Object.keys(entry).join(","))
.join(",");
// Build the return object
return {...entry, bookIssue};
});
console.log(result);
That also works if there's just one entry, of course.
Upvotes: 1