0

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.

2
  • 1
    Object.values(el).indexOf(el.name) > -1 is checking if the current el object has a name propety value that is in el - this will alaways be true, as you're grabbing a value from your object, and then checking if that value is in that same object. You probably want to compare names from both objects: ele.name === el.name. Your else if can also be made into just else (or even removed entirely) Commented Oct 5, 2021 at 8:25
  • 1
    Here you go: jsfiddle.net/1zLtu9qc Commented Oct 5, 2021 at 8:58

4 Answers 4

2

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

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

4 Comments

Edited the answer with ... operator. Missed that part
Actually I have one more query should I ask new question or can I update in the same.:: My query::Create a variable string to keep note of whether amount has been reduced or increased or removed and for which book. Ex: 'Owner removed Book dune'
@SushantRad see my anser for that check. I have incooperated that aswell.
0

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

1 Comment

Here's a shorter way: jsfiddle.net/1zLtu9qc
0

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

1 Comment

Wrong result. Right way: jsfiddle.net/1zLtu9qc
0

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.
  • To find the removed book, filter the oldBookDetails array and check nodes that are not prtesent in newBookDetails

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 };
  }
});
const removedBooks = oldBookDetails.filter((book) => !newBookDetails.find((node) => node.name === book.name));
console.log(bookModified);
console.log(removedBooks);

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.