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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:09:30  来源:igfitidea点击:

Nested forEach: Uncaught TypeError: data.forEach is not a function

javascript

提问by Non

I don't want to use any for loop or any of the regular loops, I am trying to use a forEachbut 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

datais an object - forEachonly runs on Array types. You have to use a for..inloop to iterate the keys of an object.

data是一个对象 -forEach仅在 Array 类型上运行。您必须使用for..in循环来迭代对象的键。