JavaScript 功能无效。 indexOf 未定义
JavaScript function is not working. indexOf is undefined
function filteredArray(arr, elem) { let newArr = [];
Loops through every element of the nested array.
for (let i=0;i<arr.length;i++){
for (let j=0;j<arr[i].length;j++){
If the value on the iteration is equal to the argument passed, it is supposed to set a variable x to be equal to the value of the nested array during the ongoing iteration
if (arr[i][j]==elem){
let x = indexOf(arr[i][j]);
It is supposed to remove the element with index equal to the variable x.
arr[i][j].splice(x,1);
Then it is supposed to push the remained of the nested array to the new array and then subsequently return the new array.
newArr[i].push(...arr[i][j]);
}
}
}
console.log(newArr);
return newArr;
}
但是有一个错误说 'indexOf is not defined'
我不明白为什么它不起作用。 return indexOf 每次迭代都未定义。请看评论。
如果您不介意,请分享您对我的代码的看法。
indexOf
是一个 array/string
方法,可以像 array.indexOf(element)
这样的数组调用。在您的情况下,您需要通过 array
.
你也可以跳过indexOf
因为这里变量i
和j
会给出父数组和嵌套数组的相关索引
function filteredArray(arr, elem) { let newArr = [];
Loops through every element of the nested array.
for (let i=0;i<arr.length;i++){
for (let j=0;j<arr[i].length;j++){
If the value on the iteration is equal to the argument passed, it is supposed to set a variable x to be equal to the value of the nested array during the ongoing iteration
if (arr[i][j]==elem){
let x = indexOf(arr[i][j]);
It is supposed to remove the element with index equal to the variable x.
arr[i][j].splice(x,1);
Then it is supposed to push the remained of the nested array to the new array and then subsequently return the new array.
newArr[i].push(...arr[i][j]);
}
}
}
console.log(newArr);
return newArr;
}
但是有一个错误说 'indexOf is not defined'
我不明白为什么它不起作用。 return indexOf 每次迭代都未定义。请看评论。 如果您不介意,请分享您对我的代码的看法。
indexOf
是一个 array/string
方法,可以像 array.indexOf(element)
这样的数组调用。在您的情况下,您需要通过 array
.
你也可以跳过indexOf
因为这里变量i
和j
会给出父数组和嵌套数组的相关索引