【发布时间】:2019-10-26 03:30:10
【问题描述】:
我已经建立了一个数组对象结构如下
nestedArray = [ { source : "a" , target : "b", table : "ab" },
{ source : "c" , target : "d", table : "cd" },
{ source : "a" , target : "d", table : "cd" },
{ source : "a" , target : "f", table : "cd" }];
我写了下面的代码来删除source是a的所有嵌套对象。
nestedArray.forEach(function(element) {
if(element.source == "a") {
nestedArray.splice(nestedArray.indexOf(element), 1) ;
}
});
但是当我尝试在上面的代码之后打印数组时,我仍然可以看到source 为a 的对象。
> > nestedArray
[ { source: 'c', target: 'd', table: 'cd' },
{ source: 'a', target: 'f', table: 'cd' } ]
> >
我在这里遗漏了什么吗?任何建议都非常感谢。
【问题讨论】:
-
使用
filter()而不是forEach()。
标签: javascript