Skip to content
Advertisement

Concatenate array of object inside array of object

Suppose I have array of object as:

const bookDetails = [
    {
        "bookId": "1235",
        "emailId": "samplek@gmail.com",
        "bookIssue": [{"Book not properly aligned": true, "some issue1": true}]
    },
    {
        "bookId": "1235",
        "emailId": "s@gmampleail.com",
        "bookIssue": [{"some issues with book": true, "some issue2": true}]
    }]

I want the O/P as:

[
    {"bookId": "1235", "emailId": "samplek@gmail.com", "bookIssue": "Book not properly aligned,some issue1"},
    {"bookId": "1235", "emailId": "s@gmampleail.com", "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":"samplek@gmail.com","bookIssue":"0"},
{"bookId":"1235","emailId":"s@gmampleail.com","bookIssue":"0"}]

What could be the issue, is there any other way to achieve this?

Advertisement

Answer

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":"samplek@gmail.com","bookIssue":[{"Book not properly aligned": true,"some issue1":true}]},
    {"bookId":"1235","emailId":"s@gmampleail.com","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":"samplek@gmail.com","bookIssue":[
        {"Book not properly aligned": true,"some issue1":true},
        {"another issue": true,"yet another issue":true}
    ]},
    {"bookId":"1235","emailId":"s@gmampleail.com","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.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement