javascript使用forEach嵌套数组

qts

如果我有nested array以下内容:

var attendees={attendees :[{name: John},{name: Terry}]}

如何使用forEach函数在名称中循环我试过了:

attendees.forEach(function(attendees){
    console.log(attendees.name):
});

但是它不会遍历子数组,而只是给我:

[{name: John},{name: Terry}]

感谢帮助!

马科斯·佩雷斯·古德(MarcosPérezGude)

与另一个foreach,当检测到数组时:

attendees.forEach(function(attendee){
   if(attendee.isArray()) {
      attendee.forEach(function(subattendee) {
         console.log(subattendee):
      });
   }
});

小心嵌套变量。对于已读数组和函数赋值变量,您具有相同的名称。

祝你好运

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章