Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
Keep track of object removed, added after comparison between two array of object in a string

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!