Javascript TypeError:无法读取未定义的属性“indexOf”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41895890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:44:11  来源:igfitidea点击:

Javascript TypeError: Cannot read property 'indexOf' of undefined

javascriptjquery

提问by Ali Hesari

In this code I want to remove an element from the cart_productsarray.

在这段代码中,我想从cart_products数组中删除一个元素。

var cart_products = ["17^1", "19^1", "18^1"];
var product = 17;

$.each(cart_products,function(key, item) {
    if(item.indexOf(product+"^") !== -1){
        cart_products.splice(key, 1);
    }
});

But I get this error in Google Chrome console:

但是我在 Google Chrome 控制台中收到此错误:

Uncaught TypeError: Cannot read property 'indexOf' of undefined

未捕获的类型错误:无法读取未定义的属性“indexOf”

Is there something wrong with the code?

代码有问题吗?

Thanks for your help.

谢谢你的帮助。

回答by T.J. Crowder

The problem is that you're modifyingthe array while jQuery's $.eachis looping over it, so by the time it gets to the end, the entry that used to be at index 2 is no longer there. (I admit I'm a bit surprised $.eachbehaves that way, but I haven't used $.eachin at least five years, so...)

问题是您在jQuery循环遍历数组时正在修改数组$.each,因此当它结束时,曾经位于索引 2 的条目不再存在。(我承认我有点惊讶这样的$.each行为,但我$.each至少有五年没用过了,所以......)

If the goal is to remove matches from the array, the better choice is filter:

如果目标是从数组中删除匹配项,更好的选择是filter

var cart_products = ["17^1", "19^1", "18^1"];
var product = 17;

cart_products = cart_products.filter(function(item) {
    return item.indexOf(product+"^") === -1;
});
console.log(cart_products);

...or alternately if it's important to modify the array in-place rather than creating a new one use a boring forloop as Andreas points outlooping backwardthrough the array so it doesn't matter when you remove things:

...或者,如果就地修改数组而不是创建新数组很重要,请使用无聊的for循环,因为 Andreas 指出在数组中向后循环,因此删除内容时无关紧要:

var cart_products = ["17^1", "19^1", "18^1"];
var product = 17;

var target = product + "^";
for (var index = cart_products.length - 1; index >= 0; --index) {
  if (cart_products[index].indexOf(target) !== -1) {
    cart_products.splice(index, 1);
  }
}
console.log(cart_products);

回答by shadymoses

First of all, you don't need to use a jQuery each for this. Second, it's not a great idea to alter an array that you are operating on. If you're trying to remove elements from an array, use filter. Filter has the following signature:

首先,您不需要为此分别使用 jQuery。其次,更改您正在操作的数组并不是一个好主意。如果您尝试从数组中删除元素,请使用过滤器。过滤器具有以下签名:

someArray.filter(function(item, index, array) {
  // return a value that is truthy to keep an item or falsey to remove it
})

Filter returns a new array with only the values that match what you want. That means you don't mess with your original array, which is a good idea anyways. In your case it would look like this:

Filter 返回一个新数组,其中仅包含与您想要的值匹配的值。这意味着您不会弄乱原始数组,无论如何这是一个好主意。在您的情况下,它看起来像这样:

var filteredProducst = cart_products.filter(function(item) {
  return item.indexOf(product + "^")
})