Home > OS >  Create new array based on comparison between two array of objects
Create new array based on comparison between two array of objects

Time:10-05

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.

CodePudding user response:

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

CodePudding user response:

Array.map will help you to generate the new array as per your requirement.

Logic

  • Loop through newBookDetails array.
  • Check if node from newBookDetails is present in oldBookDetails
  • If no return the newBook node with is_modified as true
  • If yes. return the node with is_modified value as a boolean that compare the new book and old book price.

const 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 };
  }
});
console.log(bookModified);

CodePudding user response:

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

CodePudding user response:

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 = oldBookDetails
  .filter((oldBookDetail) => newBookDetails.findIndex((bookDetails) => bookDetails.name === oldBookDetail.name) > -1)
  .map((oldBookDetails) => {
  const newDetails = newBookDetails.find((bookDetails) => bookDetails.name === oldBookDetails);
  return {...oldBookDetails, ...newDetails};
});

console.log(bookModified);

Could be done better though, if you spend more time in optimizing and sharpening

  • Related