DEV Community

Cover image for How to Loop JS Object Values
Asela
Asela

Posted on • Updated on

How to Loop JS Object Values

So, How to Loop JavaScript Object Values ?

Here It's

const myJSON = '{"name":"John", "age":30, "car":null}';
const myObj = JSON.parse(myJSON);

let text = "";
for (let x in myObj) {
  text += myObj[x] + ", ";
}
Enter fullscreen mode Exit fullscreen mode

Updated:
I find simple one line code for that without use loop from comment section.

const text = Object.values(myObj).join(',')
Enter fullscreen mode Exit fullscreen mode

Comment By rancy98 , Thank you rancy
So Simple, Isn't it? Have a Nice day and Don't forget to follow.

Top comments (4)

Collapse
 
rancy98 profile image
rancy98


const text = Object.values(myObj).join(', ')

Collapse
 
zaselalk profile image
Asela

Thank you !, I updated my post according to your comment

Collapse
 
nombrekeff profile image
Keff

You can highlight the code, by adding js after the openign code block, see the editor guide here

There is also a little sytax error:

for (let x in myObj) {
  text = += myObj[x] + ", ";
}
Enter fullscreen mode Exit fullscreen mode

Notice, you are doing = +=, it should be just +=

Have a nice day too!

Collapse
 
zaselalk profile image
Asela

Thank you for commenting about mistake ! I updated my post according to this.