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}
]
let bookModified = []
I want to create new array of Object by comparing newBookDetails with oldBookDetails,
and check if any name is removed newDetails, don't push into the bookModified array,
if amount has been changed then push newBookDetails object into bookModified with is_modified: true and if newBookDetails amount is same push into bookModified array with is_modified:false.
For this I tried as:
newBookDetails.forEach((el) => {
oldBookDetails.forEach((ele) => {
if(Object.values(el).indexOf(el.name) > -1) {
if(el.amount === ele.amount) {
bookModified.push(el)
}else if(el.amount != ele.amount) {
el.is_modified = true
bookModified.push(el)
}
} else if(Object.values(el).indexOf(el.name) === -1) {
//this loops as all are not equal....i am stuck from this part.
}
})
})
Expected O/P :
console.log(newBookDetails)
[
{'name':'Harry pottar','amount':15, is_modified: true},
{'name':'LOTR','amount':20, is_modified: false},
{'name':'HR','amount':15, is_modified: true}
]
If anyone needs any further information please do let me know.
You can do it using map() and find():
map() on the second array and try to find the book in the first one using property name. If not found pass in modified true, else check the amount and pass modified value accordingly.
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 }
];
let bookModified = newBookDetails.map((x) => {
let foundBook = oldBookDetails.find(old => old.name === x.name);
if (foundBook) {
if (foundBook.amount !== x.amount)
return { ...x, is_modified: true };
else
return { ...x, is_modified: false };
}
else
return { ...x, is_modified: true };
});
console.log(bookModified);
Array.map will help you to generate the new array as per your requirement.
Logic
newBookDetails array.newBookDetails is present in oldBookDetailsis_modified as trueis_modified value as a boolean that compare the new book and old book price.oldBookDetails array and check nodes that are not prtesent in newBookDetailsconst oldBookDetails = [ {'name':'Harry pottar','amount':10, is_modified: false}, {'name':'LOTR','amount':20, is_modified: false}, {'name':'dune','amount':15, is_modified: false}];
const newBookDetails = [ {'name':'Harry pottar','amount':15}, {'name':'LOTR','amount':20}, {'name':'HR','amount':15} ];
const bookModified = newBookDetails.map((node) => {
const oldBookNode = oldBookDetails.find((item) => item.name === node.name);
if (oldBookNode) {
return { ...node, is_modified: oldBookNode.amount !== node.amount }
} else {
return { ...node, is_modified: true };
}
});
const removedBooks = oldBookDetails.filter((book) => !newBookDetails.find((node) => node.name === book.name));
console.log(bookModified);
console.log(removedBooks);
You can do with below snippet
newBookDetails.forEach(newBook => {
let found = false;
oldBookDetails.forEach(oldBook => {
if (oldBook.name === newBook.name) {
if (oldBook.amount === newBook.amount) {
bookModified.push({
'name': newBook.name,
'amount': newBook.amount,
is_modified: false
})
} else {
bookModified.push({
'name': newBook.name,
'amount': newBook.amount,
is_modified: true
})
}
found = true;
}
})
if (!found) {
bookModified.push({
'name': newBook.name,
'amount': newBook.amount,
is_modified: true
})
}
})