Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda