如何搜索对象数组,并防止如果值存在多个Angular 8

发布于 2025-02-03 05:04:28 字数 1316 浏览 3 评论 0 原文

单击“ Addmore”按钮,我将 newData 对象推入 olddata ,但是在按下之前,我需要检查以下条件。

我想检查 productType processTechType familymake lineId 列值不止一次 olddata 数组,然后会丢弃错误。

以下是我的数据:

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"}
];

我在下面做了下面的代码,并且它非常有效,但是我的代码将整个对象与 olddata 数组进行比较, 但是我只想检查使用 newData 对象的4列, olddata 数组。

谁能帮我做到这一点。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞美少年 2025-02-10 05:04:28

您可以这样做:

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

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);
    }
}
梦在深巷 2025-02-10 05:04:28

使用查找(*):

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

(*)看到使用“如果”就像“普通循环”。 使用比较 if(!

函数返回“未定义”(如果找不到的话),因此我们 。这就是我使用“加入” - 返回字符串的原因,因此我比较了两个字符串。

更新使用过滤器

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:比较说,如果其中一个属性相等。如果您需要所有属性是同等用途&amp;&amp; 而不是 ||

**在第二个代码示例中修复语法错误

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文