【问题标题】:Javascript ForEach object in bracket array?括号数组中的Javascript ForEach对象?
【发布时间】:2019-05-06 10:21:14
【问题描述】:

我有一个如下所示的数组:

{ messages:
   [ { username: 'wef', message: 'f', type: 'chat' },
     { username: 'wef', message: 'we', type: 'chat' },
     { username: 'wef', message: 'wefwefwef', type: 'chat' } ] }

如何循环遍历messages 中的所有对象?我尝试做一个 for 循环,但这只是发送数组本身而不是每个对象。

感谢您的帮助

【问题讨论】:

  • 你的意思是,你想循环遍历对象的每个属性?
  • @Eddie — 他们想要遍历数组中作为消息值的所有对象。
  • “我尝试做一个 for 循环,但这只是发送数组本身而不是每个对象。” — for 循环很好。当您尝试使用一个时,我们无法判断您做错了什么,因为您忘记包含 minimal reproducible example

标签: javascript loops for-loop


【解决方案1】:

看看

var obj = { messages:
   [ { username: 'wef', message: 'f', type: 'chat' },
     { username: 'wef', message: 'we', type: 'chat' },
     { username: 'wef', message: 'wefwefwef', type: 'chat' } 
]
 }
obj.messages.map(({username, message,type}) => console.log("username, message,type", username, message,type))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-24
    • 2015-03-26
    • 2015-12-07
    • 2021-11-09
    • 2011-07-05
    • 1970-01-01
    相关资源
    最近更新 更多