Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过匹配ID检查json数组中是否已存在项_Javascript_Jquery_Json_For Loop - Fatal编程技术网

Javascript 通过匹配ID检查json数组中是否已存在项

Javascript 通过匹配ID检查json数组中是否已存在项,javascript,jquery,json,for-loop,Javascript,Jquery,Json,For Loop,所以我有这个购物车,作为JSON [{"tuote":{"id":"2","name":"Rengas 2","count":16,"price":"120.00"}},{"tuote":{"id":"1","name":"Rengas 6","count":"4","price":"25.00"}},{"tuote":{"id":"4","name":"Rengas 4","count":"4","price":"85.00"}}] 因此,我想防止在那里有两个相同的值,并通过它们的ids匹

所以我有这个购物车,作为JSON

[{"tuote":{"id":"2","name":"Rengas 2","count":16,"price":"120.00"}},{"tuote":{"id":"1","name":"Rengas 6","count":"4","price":"25.00"}},{"tuote":{"id":"4","name":"Rengas 4","count":"4","price":"85.00"}}]

因此,我想防止在那里有两个相同的值,并通过它们的
id
s匹配它们

这是我当前的解决方案(buggy作为蟑螂,并没有真正完成这项工作),因为它唯一有效的时间是匹配值在JSON字符串中的第一个

for (var i = 0; i < ostoskori.length; i++) {

    if (ostoskori[i].tuote.id == tuoteID) {
        addToExisting(tuoteID, tuoteMaara); //this doesn't matter, it works.
        break //the loop should stop if addToExisting() runs
    }

    if (ostoskori[i].tuote.id != tuoteID) {
        addNew(tuoteID, tuoteNimi, tuoteMaara, tuoteHinta); //this doesn't matter, it works.
        //break 

        //adding break here will stop the loop, 
        //which prevents the addToExisting() function running
    }
}
for(变量i=0;i
ostoskori
是json,如果你想知道的话。正如您可能看到的,对于JSON中的每个项目,
addNew()
运行的次数越多

因此,基本上,如果JSON具有与
tuoteID
相同的
id
值,
addToExisting()
应该运行。如果JSON没有与
tuoteID
相同的值,请运行
addNew()

但是怎么做呢?

您可以用它来检查id是否已经存在。
some
的美妙之处在于:

如果找到这样一个元素,一些元素会立即返回true

如果你是为了迎合较老的浏览器,页面底部会有一个多边形填充

function hasId(data, id) {
  return data.some(function (el) {
    return el.tuote.id === id;
  });
}

hasId(data, '4'); // true
hasId(data, '14'); // false
因此:


如果obj id不在阵列中,您可以维护正在添加的id数组,并为下一个obj插入。这似乎是一个完美但紧凑的解决方案!但有一个问题是:如果我输入的
4
没有引号,它将导致false。是因为JSON中的
id
不是
nt
?是的。数据中的id(顺便说一句,当你解析它时它不再是JSON)是一个字符串。好的,谢谢,你救了我一天!(我正要退出并将键盘扔出窗口)您可以将
==
更改为
==
,然后不必输入整数。但我通常建议在可能的情况下使用严格平等。这实际上是一个比我的尝试更轻的选择!我现在可以摆脱for循环了。
if (hasId(data, '4')) {
  addToExisting();
} else {
  addNew();
}