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

202
Views
How to search array of objects and prevent pushing new row if value is exist more than one Angular 8

On click of AddMore button, I am pushing newData object into oldData, but before pushing I need to check below condition.

I want to check if productType, processTechType, familyMake and lineId columns values are exist more than once in oldData array, then it will throw error.

Below is my data:

const newData = {"processTypeId":0,"productType":"Savoury","processTechType":"Mustard","familyMake":["Jelly","Noodles"],"lineId":["R_X002_ACCESS","R_X002_BIB"],"batchSize":"4"}

const oldData = [
    {"processTypeId":0,"productType":"Savoury","processTechType":"Mustard","familyMake":["Jelly","Noodles"],"lineId":["R_X002_ACCESS","R_X002_BIB"],"batchSize":"1"},
    {"processTypeId":0,"productType":"Savoury","processTechType":"Mustard","familyMake":["Jelly","Noodles"],"lineId":["R_X002_ACCESS","R_X002_BIB"],"batchSize":"4"}
];

I did below code and it is perfectly working, but my code is comparing whole object with oldData array, but I want to check for only 4 column of newData object with oldData array.

Can anyone help me to do this.

addNewType(rowdata) {
    const newData = JSON.stringify(rowdata.value);
    const oldData = JSON.stringify(this.items.value);   
    if ((oldData.split(newData).length - 1) >= 2) {
     console.log("Item exist");
    } else {
      this.isRowExist = false;
      this.addItem(this.createItem(null));
    }
  }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

you can do it like this:

addNewType(newData) {
    let isDuplicate = false;
    for(let data of this.oldData){
        if(data.productType == newData.productType ||
           data.processTechType == newData.processTechType ||
           data.familyMake == newData.familyMake ||
           data.lineId == newData.lineId){
            isDuplicate = true;
            break;
        }
    }
    if(isDuplicate){
        // Do nothing
    }else{
        this.oldData.push(newData);
    }
}
about 4 years ago · Juan Pablo Isaza Report

0

Using a find (*):

addNewType(newData) {
    const el=this.oldData.find(data=>
           data.productType == newData.productType ||
           data.processTechType == newData.processTechType ||
           data.familyMake.join('|') == newData.familyMake.join('|')) ||
           data.lineId == newData.lineId)
   if (!el)
   {
         this.oldData.push(newData)
   }
}

(*) See that using an "if" is like a "normal loop". The function return "undefined" if not find, so we use the comparison if (!el){...}

NOTE1: when we need compare two arrays we need compare the "value" of the arrays. This is the reason I use "join" -that return an string-, so I compare the two strings.

Update using filter

addNewType(newData) {
    const elements = this.oldData.filter(data=>data.productType == newData.productType || data.processTechType == newData.processTechType || data.familyMake.join('|') == newData.familyMake.join('|') || data.lineId == newData.lineId)
    if (elements.length <= 0) {
        this.oldData.push(newData)
    } else {
// add check here in case of already existance
        console.log("already exists " + elements.length + " elements equals");
       }
}

NOTE2: the comparison says that if one of the properties are equal. If you need that all the properties was equal use && instead ||

** fixing syntax errors in second code sample

about 4 years ago · Juan Pablo Isaza Report
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!