如何从forEach循环中的数组中删除元素? [英] How to remove element from array in forEach loop?
问题描述
我试图从 forEach
循环中删除一个数组中的元素,但是遇到了我见过的标准解决方案的麻烦。
这就是我目前正在尝试的:
review。 forEach(function(p){
if(p ==='\\\• \\\• \\\•'){
console.log('YippeeeE !!!!!!!!!! !!!!!!')
review.splice(p,1);
}
});
我知道这是进入如果
因为我在控制台中看到 YippeeeeeE !!!!!!!!!!!!!
。
我的问题:我知道我的for循环,如果逻辑是合理的,但我试图从数组中删除当前元素失败。
更新: 试了Xotic750的答案,不会被删除: 这是我的代码中的函数: 这里是数组仍然不被删除的输出: 显然,它按照指示进入if语句,但是很显然,[...]•仍然存在。 看起来您正在尝试这样做? 对于没有与相邻数组项目相同值的2个简单情况,可以正常工作,其他方面您也有这个问题。 那么,我们能做些什么呢? n迭代和变异数组?那么通常的解决方案是反向工作。使用ES3 虽然,但您可以使用对于糖,如果优选的话bb 好的,但是你想要使用ES5迭代方法。那么和选项将使用 Array.prototype.filter ,但这不会改变原始数组,但创建一个新的,所以虽然你可以得到正确的答案,这不是什么您似乎已经指定。 我们也可以使用ES5 Array.prototype.reduceRight ,而不是因为它的迭代属性,即反向迭代。 b $ b 或者我们可以使用ES5 Array.protoype.indexOf 就像这样。 但是您特别想使用ES5 Array.prototype.forEach ,那我们该怎么办?那么我们需要使用 Array.prototype.slice 来制作数组的浅表副本和 Array.prototype.reverse 所以我们可以反向工作来改变原始数组。 在上述所有的是,如果你从数组中剥离 NaN 那么和equals相比是不行的,因为在Javascript中 所以我们有了这个解决方案,案例。第一个代码示例仍然正确,但正如所述,这不是没有问题的。 I am trying to remove an element in an array in a This is what I'm currently trying: I know it's getting into the MY PROBLEM: I know that my for loop and if logic are sound, but my attempt to remove the current element from the array is failing. UPDATE: Tried out Xotic750's answer, and the element is still not being removed: Here is the function in my code: Here is the output where the array is still not removed: So obviously it is going into the if statement as directed, but it's also obvious that the [• • •] is still there. It looks like you are trying to do this? Iterate and mutate an array using Array.prototype.splice
Which works fine for simple case where you do not have 2 of the same values as adjacent array items, other wise you have this problem.
So what can we do about this problem when iterating and mutating an array? Well the usual solution is to work in reverse. Using ES3 while but you could use for sugar if preferred
Ok, but you wanted you wanted to use ES5 iteration methods. Well and option would be to use Array.prototype.filter but this does not mutate the original array but creates a new one, so while you can get the correct answer it is not what you appear to have specified. We could also use ES5 Array.prototype.reduceRight, not for its reducing property by rather its iteration property, i.e. iterate in reverse.
Or we could use ES5 Array.protoype.indexOf like so.
But you specifically want to use ES5 Array.prototype.forEach, so what can we do? Well we need to use Array.prototype.slice to make a shallow copy of the array and Array.prototype.reverse so we can work in reverse to mutate the original array.
Finally ES6 offers us some further alternatives, where we do not need to make shallow copies and reverse them. Notably we can use Generators and Iterators. However support is fairly low at present.
Something to note in all of the above is that, if you were stripping NaN from the array then comparing with equals is not going to work because in Javascript So there we have it, a more complete answer with solutions that still have edge cases. The very first code example is still correct but as stated, it is not without issues. 这篇关于如何从forEach循环中的数组中删除元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
pre $ review.forEach(function(item,index,object)){
if(item ==='\\\• \\\• \\\•'){
console.log('YippeeeE !!! ')
object.splice(index,1);
}
console.log('['+ item +']' );
});
[Scott McNeil]
[已审核4个月前]
[三菱令人惊叹!!!]
YippeeeE !!!! !!!!!!!!!!!!
$ b $ p
var pre = document.getElementById('out');函数日志(结果){pre.appendChild(document.createTextN ode(result +'\\\
'));} var review = ['a','b','c','b','a']; review.forEach(function(item,index,object) {if(item ==='a'){object.splice(index,1); }}); log(review); < pre id =out>< / pre>
$ b var pre var document.getElementById('out'); function log(result){pre.appendChild(document.createTextNode(result +'\\\
'));} var review = ['a','a','b' ,'c','b','a','a']; review.forEach(function(item,index,object){if(item ==='a'){object.splice(index,1) ;}}); log(review); < pre-id =out>< / pre>
var pre = document.getElementById('out'); function log(result){pre.appendChild(document.createTextNode(result +'\\\
'));} var review = ['a','a','b','c','b','a','a']; review.reduceRight(函数(acc,item,index,object){if ==='a'){object.splice(index,1); (code> ); < pre id =out>< / pre>
$ b var pre var document.getElementById('out'); function log(result){pre.appendChild(document.createTextNode(result +'\\\
'));} var review = ['a','a','b' ,'c','b','a', A],索引= review.indexOf( A);而(!索引== -1){review.splice(指数,1); index = review.indexOf('a');} log(review);
< pre id =out>< / pre>
var pre = document.getElementById('out'); function log(result){pre.appendChild(document.createTextNode(result +'\\\
'));} var review = ['a','a', (b),c,b,a,a,]; review.slice()。reverse()。forEach(function(item,index,object){if(item ==='a '){review.splice(object.length - 1 - index,1); }}); log(review); < pre id =out>< / pre>
< pre id = out>< / pre>
NaN === NaN
是错误的。但是,我们将忽略解决方案,因为它是另一个未明确说明的优势。
forEach
loop, but am having trouble with the standard solutions I've seen. review.forEach(function(p){
if(p === '\u2022 \u2022 \u2022'){
console.log('YippeeeE!!!!!!!!!!!!!!!!')
review.splice(p, 1);
}
});
if
because I'm seeing YippeeeeeE!!!!!!!!!!!!!
in the console. review.forEach(function (item, index, object) {
if (item === '\u2022 \u2022 \u2022') {
console.log('YippeeeE!!!!!!!!!!!!!!!!')
object.splice(index, 1);
}
console.log('[' + item + ']');
});
[Scott McNeil]
[reviewed 4 months ago]
[ Mitsubishi is AMAZING!!!]
YippeeeE!!!!!!!!!!!!!!!!
[• • •]
var pre = document.getElementById('out');
function log(result) {
pre.appendChild(document.createTextNode(result + '\n'));
}
var review = ['a', 'b', 'c', 'b', 'a'];
review.forEach(function(item, index, object) {
if (item === 'a') {
object.splice(index, 1);
}
});
log(review);
<pre id="out"></pre>
var pre = document.getElementById('out');
function log(result) {
pre.appendChild(document.createTextNode(result + '\n'));
}
var review = ['a', 'a', 'b', 'c', 'b', 'a', 'a'];
review.forEach(function(item, index, object) {
if (item === 'a') {
object.splice(index, 1);
}
});
log(review);
<pre id="out"></pre>
var pre = document.getElementById('out');
function log(result) {
pre.appendChild(document.createTextNode(result + '\n'));
}
var review = ['a' ,'a', 'b', 'c', 'b', 'a', 'a'],
index = review.length - 1;
while (index >= 0) {
if (review[index] === 'a') {
review.splice(index, 1);
}
index -= 1;
}
log(review);
<pre id="out"></pre>
var pre = document.getElementById('out');
function log(result) {
pre.appendChild(document.createTextNode(result + '\n'));
}
var review = ['a', 'a', 'b', 'c', 'b', 'a', 'a'];
review.reduceRight(function(acc, item, index, object) {
if (item === 'a') {
object.splice(index, 1);
}
}, []);
log(review);
<pre id="out"></pre>
var pre = document.getElementById('out');
function log(result) {
pre.appendChild(document.createTextNode(result + '\n'));
}
var review = ['a', 'a', 'b', 'c', 'b', 'a', 'a'],
index = review.indexOf('a');
while (index !== -1) {
review.splice(index, 1);
index = review.indexOf('a');
}
log(review);
<pre id="out"></pre>
var pre = document.getElementById('out');
function log(result) {
pre.appendChild(document.createTextNode(result + '\n'));
}
var review = ['a', 'a', 'b', 'c', 'b', 'a', 'a'];
review.slice().reverse().forEach(function(item, index, object) {
if (item === 'a') {
review.splice(object.length - 1 - index, 1);
}
});
log(review);
<pre id="out"></pre>
var pre = document.getElementById('out');
function log(result) {
pre.appendChild(document.createTextNode(result + '\n'));
}
function* reverseKeys(arr) {
var key = arr.length - 1;
while (key >= 0) {
yield key;
key -= 1;
}
}
var review = ['a', 'a', 'b', 'c', 'b', 'a', 'a'];
for (var index of reverseKeys(review)) {
if (review[index] === 'a') {
review.splice(index, 1);
}
}
log(review);
<pre id="out"></pre>
NaN === NaN
is false. But we are going to ignore that in the solutions as it it yet another unspecified edge case.