我已经能够使用“for”循环和“for in”循环找出拼接,但不能使用“for of”循环。是否可以?这是我的起始代码...我可以改变什么来使其工作有什么想法吗?
let array = [ 'a', 'b', 'c' ];
function remove( letter ){
for( let item of array ){
if( item === letter ){
parkedCars.splice ( item, 1 );
}
}
}
remove( 'b' );
console.log( array );
最佳答案
您可以在 Array.prototype.entries()
上使用 for...of
循环,然后使用 splice() 检查值并按索引删除项目
.
let array = ['a', 'b', 'c'];
function remove(arr, letter) {
for (let [index, item] of arr.entries()) {
if (item === letter) arr.splice(index, 1);
}
}
remove(array, 'b');
console.log(array);
关于javascript - 是否可以使用 javascript 中的 'for of' 循环从数组中拼接项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49499161/