Javascript loop through object array?

后端 未结 9 2162
执念已碎
执念已碎 2020-11-28 04:55

I am trying to loop through the following:

{
    \"messages\": [{
        \"msgFrom\": \"13223821242\",
        \"msgBody\": \"Hi there\"
    }, {
        \"         


        
9条回答
  •  生来不讨喜
    2020-11-28 05:37

    Iterations

    Method 1: forEach method

    messages.forEach(function(message) {
       console.log(message);
    }
    

    Method 2: for..of method

    for(let message of messages){
       console.log(message);
    }
    

    Note: This method might not work with objects, such as:

    let obj = { a: 'foo', b: { c: 'bar', d: 'daz' }, e: 'qux' }
    

    Method 2: for..in method

    for(let key in messages){
           console.log(messages[key]);
     }
    

提交回复
热议问题