Suppose I have two array of Object as,
let oldBookDetails = [
{'name':'Harry pottar','amount':10, is_modified: false},
{'name':'LOTR','amount':20, is_modified: false},
{'name':'dune','amount':15, is_modified: false}
]
let newBookDetails = [
{'name':'Harry pottar','amount':15},
{'name':'LOTR','amount':20},
{'name':'HR','amount':15}
]
With help from stack overflow member @Tushar Shahi, with help of below solution, I get objects which has been modified into new array by comparing oldBookDetails and newBookDetails, I tried as,
let componentRemovedNote = "The centre has added ";
let componentAddedNote = "The center has removed ";
let bookModified = newBookDetails.map((x) => {
let foundBook = oldBookDetails.find((old) => old.name === x.name);
if (foundBook) {
if (foundBook.amount !== x.amount) {
if (foundBook.amount < x.amount) {
componentAddedNote += `${foundBook.name} - ${foundBook.amount}`;
return { ...x, is_modified: true };
} else {
componentRemovedNote += ` ${x.name} - ${x.amount} `;
return { ...x, is_modified: true };
}
} else return { ...x, is_modified: false };
} else {
componentAddedNote += `${x.name} - ${x.amount}`;
return { ...x, is_modified: true };
}
});
Expected result of bookDetails array is correct, but I also want to keep notes as:
Owner added Harry Pottar - 10 (original value), HR - 15 (new value)
Owner removed dune - 15
Solution I tried is giving me incurred value in componentRemovedNote and componentAddedNote.
Please let me know if anyone needs any further information.