【问题标题】:splice is not removing the first index from array of objectssplice 没有从对象数组中删除第一个索引
【发布时间】:2018-12-04 16:03:18
【问题描述】:

我有两个对象数组,我想将第一个数组的对象与第二个数组的对象进行比较。如果它们匹配,我使用splice 从第二个数组中删除对象。

我有以下代码

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

所以,如果existing

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

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

那么,来自newdatafeat.features 的 Mike 应该被移除。

错误是newdatafeat.features数组中索引为0的每一项都没有被删除。在循环中,我可以看到 index to remove - 0,但 Mike 从未被移除。我知道,因为如果我在循环之后console.log newdatafeat.features,Mike 就在那里

这是在 angular6 代码中。

我在这里缺少什么?

谢谢

【问题讨论】:

  • @charlietfl 是的,“Mike”有id:1, cat:true,就像来自existing 的“John”。 id 和 cat 匹配,因此必须删除 Mike。我只比较 id 和 cat,而不是名称。再次检查if 语句。谢谢

标签: javascript arrays angular6 array-splice


【解决方案1】:

我不得不清理一些代码,但看起来你的代码运行良好。它确定了一个要删除的元素,称为 slice 并且它已经消失了。

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

【讨论】:

  • 好的,那么我的代码中的包是什么?有什么建议么?谢谢
  • 我将 newdatafeat 重命名为 newdata。我将var newdata 从一个数组更新为一个对象。
  • 感谢您对newdatafeat 对象的评论。我编辑了我的问题。但是我的问题仍然存在。我不知道它是由 angular 还是 openlayers 引起的,我什至不知道如何进行调试。谢谢
  • @codebot 那么在这个答案中运行 sn-p 会产生正确的结果吗?
  • 为什么我被否决了?我修复了样本并且它有效。如果您更改了问题,请给我一个编辑的机会。不要对我投反对票。
【解决方案2】:

这里的主要问题是您在循环中迭代一个数组,但是您在同一个循环中从该数组中删除项。所以索引经常会被关闭,错误的元素或者没有元素会被移除。用简单的例子可能很难重现,但是你去吧:

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!

解决此问题的一种简单方法是手动处理循环,如果删除元素,则手动更改索引。

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!

【讨论】:

    猜你喜欢
    • 2019-10-14
    • 2019-07-23
    • 1970-01-01
    • 2018-03-12
    • 2017-11-24
    • 1970-01-01
    • 2018-11-25
    • 1970-01-01
    • 2018-03-15
    相关资源
    最近更新 更多