javascript 嵌套 forEach:未捕获的类型错误:data.forEach 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32359735/
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
Nested forEach: Uncaught TypeError: data.forEach is not a function
提问by Non
I don't want to use any for loop or any of the regular loops, I am trying to use a forEach
but I am getting an error
我不想使用任何 for 循环或任何常规循环,我正在尝试使用 aforEach
但出现错误
Uncaught TypeError: data.forEach is not a function
未捕获的类型错误:data.forEach 不是函数
return falsyData.map(function(data) {
data.forEach(function(key) {
if (key.match(reg)) {
return key;
}
});
});
but if I do it this way it works:
但如果我这样做,它会起作用:
return falsyData.map(function(data) {
for (var key in data) {
if (key.match(reg)) {
return key;
}
}
});
why ?
为什么 ?
回答by tymeJV
data
is an object - forEach
only runs on Array types. You have to use a for..in
loop to iterate the keys of an object.
data
是一个对象 -forEach
仅在 Array 类型上运行。您必须使用for..in
循环来迭代对象的键。