我试图在forEach循环中删除数组中的元素,但我遇到了我见过的标准解决方案的问题.
这就是我目前正在尝试的:
review.forEach(function(p){
if(p === '\u2022 \u2022 \u2022'){
console.log('YippeeeE!!!!!!!!!!!!!!!!')
review.splice(p, 1);
}
});
Run Code Online (Sandbox Code Playgroud)
我知道它正在进入,if因为我YippeeeeeE!!!!!!!!!!!!!在控制台中看到了.
我的问题:我知道我的for循环和逻辑是合理的,但是我尝试从数组中删除当前元素是失败的.
更新:
尝试了Xotic750的答案,该元素仍未被删除:
这是我的代码中的函数:
review.forEach(function (item, index, object) {
if (item === '\u2022 \u2022 \u2022') {
console.log('YippeeeE!!!!!!!!!!!!!!!!')
object.splice(index, 1);
}
console.log('[' + item + ']');
});
Run Code Online (Sandbox Code Playgroud)
这是仍未删除数组的输出:
[Scott McNeil]
[reviewed 4 months ago]
[ Mitsubishi is AMAZING!!!]
YippeeeE!!!!!!!!!!!!!!!!
[• • •]
Run Code Online (Sandbox Code Playgroud)
所以很明显它会按照指示进入if语句,但显而易见的是[•••]仍然存在.
var people = ['alex','jason','matt'];
people.forEach(function(p){
if(p.length > 4){
//REMOVE THIS PERSON or pop it out of the list or whatever
}
});
console.log(people) //should return ['alex','matt']
Run Code Online (Sandbox Code Playgroud)
我想使用forEach循环从列表中删除一个元素.