-5

I have an array that looks like this:

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

How do I loop through all objects in messages? I tried doing a for loop but that just sends the array itself and not each object.

Thanks for any help

  • You mean, you want to loop thru each property of the object? – Eddie May 06 '19 at 10:23
  • @Eddie — They want to loop through all the objects in the array that is the value of messages. – Quentin May 06 '19 at 10:23
  • 2
    "I tried doing a for loop but that just sends the array itself and not each object." — A for loop is fine. We can't tell what you did wrong when you tried to use one because you forgot to include a [mcve] – Quentin May 06 '19 at 10:24
  • Possible duplicate of [Loop through an array in JavaScript](https://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript) – adiga May 06 '19 at 10:38

1 Answers1

-1

take a look

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))
Harish
  • 1,841
  • 1
  • 13
  • 26