admin管理员组

文章数量:1344515

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

Uncaught TypeError: data.forEach is not a function

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 ?

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

Uncaught TypeError: data.forEach is not a function

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 ?

Share Improve this question asked Sep 2, 2015 at 17:38 NonNon 8,61920 gold badges80 silver badges130 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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.

本文标签: javascriptNested forEach Uncaught TypeError dataforEach is not a functionStack Overflow