0

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?

6
  • The inner array always has just one entry? Commented Apr 5, 2021 at 10:34
  • What do you want to do with those? Commented Apr 5, 2021 at 10:38
  • Yes, it only has one entry, if you are talking about bookIssues Commented Apr 5, 2021 at 10:39
  • 2
    Your code works just fine actually. Commented Apr 5, 2021 at 10:42
  • Thank you @georg, I didn't read past the lack of markdown correctly. Commented Apr 5, 2021 at 10:43

3 Answers 3

1

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.

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

1 Comment

Thanks for pointing out ....also I needed to store the value in new variable ...so your solution worked for me...
1

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);

Comments

1

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(","));

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.