如何搜索对象数组,并防止如果值存在多个Angular 8
单击“ 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));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样做:
you can do it like this:
使用查找(*):
(*)看到使用“如果”就像“普通循环”。 使用比较
if(!
函数返回“未定义”(如果找不到的话),因此我们 。这就是我使用“加入” - 返回字符串的原因,因此我比较了两个字符串。
更新使用过滤器
Note2:比较说,如果其中一个属性相等。如果您需要所有属性是同等用途
&&
而不是||
**在第二个代码示例中修复语法错误
Using a find (*):
(*) 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
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