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

205
Visualizações
How to compare values of two objects in Angular

I am pushing row dynamically on add new button click in my form. The new data will be added in oldData array in the form of object.

I want to match all values of newData object with oldData array, if newData object with same values is available in oldData array of objects, Then code will console error else it push the row.

Can anyone please help me to make my function working.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I do it like this:

private allElementExists(newData: any[], oldData: any[]): boolean {
    for (const item of newData) {
        var verifyItem = oldData.find(x => x.id === item.id);
        if (verifyItem === undefined) {
            return false;
        }
    }
    return true;
}

Here you have a function that compares the two objets as you putted in your question

Here is an example in stackblitz using this function

https://stackblitz.com/edit/angular-xscnkf?file=src%2Fapp%2Fapp.component.html

about 4 years ago · Juan Pablo Isaza Relatório

0

If oldData keeps several objects, you can do: if(!oldData.includes(newData))

EDIT 1 (for original code in description): if both are just single objects, simple comparison will do: if(oldData! = newData)

EDIT 2 (for edited code in description):

addNewType(rowdata) {
    const newData = JSON.stringify(rowdata.value);
    const oldData = JSON.stringify(this.items.value);
    if(oldData.includes(newData)){
        console.log('This element already exists in the detail list');
    }
    else this.addItem(this.createItem(null));           
}

EDIT 3: if oldData has newData at least twice

addNewType(rowdata) {
    const newData = JSON.stringify(rowdata.value);
    const oldData = JSON.stringify(this.items.value);
    if((oldData.split(newData).length - 1) >= 2){
        console.log('This element already exists in the detail list at least TWICE');
    }
    else this.addItem(this.createItem(null));           
}

or:

addNewType(rowdata) {
    const newData = JSON.stringify(rowdata.value);
    const oldData = JSON.stringify(this.items.value);
    let newReg= new RegExp(newData, "g");
    if((oldData.match(newReg) || []).length >= 2){
        console.log('This element already exists in the detail list at least TWICE');
    }
    else this.addItem(this.createItem(null));           
}
about 4 years ago · Juan Pablo Isaza Relatório
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