简体   繁体   English

拼接未从对象数组中删除第一个索引

[英]splice is not removing the first index from array of objects

I have two arrays of objects and I want to compare the objects of the first array to the ones of the second array. 我有两个对象数组,我想将第一个数组的对象与第二个数组的对象进行比较。 If they match, I use the splice to remove the object from the second array. 如果它们匹配,我将使用splice从第二个数组中删除该对象。

I have the following code 我有以下代码

existing.forEach((existingitem, existingindex, existingfeatures) => {     
  (newdatafeat.features).forEach((newitem, newindex, newfeatures) => { 
    console.log('existing index-name --- new index-name', existingindex ,' - ',existingitem.values_.name,' - ',newindex,' - ',newitem.properties.name,'-----');
    if (existingitem.values_.id == newitem.properties.id &&  existingitem.values_.cat == newitem.properties.cat){              
      console.log(' index to remove - ', newindex); (newdatafeat.features).splice(newindex,1);              
    } 
  })
});   

So, If existing is 所以,如果existing

var existing= [
  { info: true, values_:{id:1, cat:true, name : "John"} }, 
  { info : true, values_:{id:2, cat:false, name : "Alice"} }  
];

and newdatafeat.features is newdatafeat.features

var newdatafeat= {
   status:scanned,
   features : [  { info: true, properties:{id:1, cat:true, name : "Mike"} }, 
    {  info : false, properties:{id:22, cat:false,name : "Jenny"} }  ]
};

Then, Mike from newdatafeat.features should be removed. 然后,应删除newdatafeat.features Mike。

The error is that every item of the newdatafeat.features array with index 0 is not removed. 错误是没有删除索引为0的newdatafeat.features数组的每个项目。 In the loop, I can see index to remove - 0 , but Mike is never removed. 在循环中,我可以看到index to remove - 0 ,但是Mike从未被删除。 I know, because if I console.log the newdatafeat.features after the loops, Mike is there 我知道,因为如果在循环后我执行console.log newdatafeat.features ,Mike就在那里

This is inside an angular6 code. 这是在angular6代码内部。

What am I missing here? 我在这里想念什么?

Thanks 谢谢

I had to clean up some code, but it looks like yours is working fine. 我不得不清理一些代码,但是看起来您的代码工作正常。 It identified one element to be removed, called slice and it was gone. 它确定了一个要删除的元素,称为切片,它消失了。

 var existing = [{ info: true, values_: { id: 1, cat: true, name: "John" } }, { info: true, values_: { id: 2, cat: false, name: "Alice" } } ]; var newdata = { status: "scanned", features: [ { info: true, properties: { id: 1, cat: true, name: "Mike" } }, { info: false, properties: { id: 22, cat: false, name: "Jenny" } } ] }; existing.forEach( (existingitem, existingindex, existingfeatures) => { (newdata.features).forEach((newitem, newindex, newfeatures) => { console.log('existing index-name --- new index-name', existingindex, ' - ', existingitem.values_.name, ' - ', newindex, ' - ', newitem.properties.name, '-----'); if (existingitem.values_.id == newitem.properties.id && existingitem.values_.cat == newitem.properties.cat) { console.log(' index to remove - ', newindex); (newdata.features).splice(newindex, 1); } }) }); console.log(newdata.features); 

The main problem here is that you are iterating an array in a loop, but you are removing items from that array in the same loop. 这里的主要问题是您要在一个循环中迭代数组,但是要在同一循环中从该数组中删除项目。 So the index will often be off, and the wrong element or no element will be removed. 因此索引通常会关闭,并且错误的元素或没有元素将被删除。 It can be hard to reproduce with simple examples, but here ya go: 很难用简单的示例来重现,但是在这里:

 function removeVowels(letters) { letters.forEach((element, index, arr) => { if ('aeiou'.indexOf(element) > -1) { arr.splice(index, 1); } }); } var myArray = ['a','b','c','d','e']; removeVowels(myArray); console.log(myArray); // works great! var myArray = ['a','e','c','d','b']; removeVowels(myArray); console.log(myArray); // wtf! 

A simple way to approach this problem is to handle the looping manually, and change the index manually if you DO remove an element. 解决此问题的一种简单方法是手动处理循环,如果要删除元素,则手动更改索引。

 function removeVowels(letters) { for (var i=0; i < letters.length; i++) { if ('aeiou'.indexOf(letters[i]) > -1) { letters.splice(i, 1); i--; } } } var myArray = ['a','b','c','d','e']; removeVowels(myArray); console.log(myArray); // works great! var myArray = ['a','e','c','d','b']; removeVowels(myArray); console.log(myArray); // hurray! 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM