Ask coding questions

← Back to all posts
Not Looping through Class Objects as an Array
Coding1000 (9)

Help! whenever I try to type in the code in the Xre, I want it to entirely print out the values and the keys as an array instead of that one object being shown. It is suppose to say all the values and keys as an array instead of printing out the first thing, try commenting out the first console to hide the object https://repl.it/@Coding1000/BadOnerlookedMolecule

Commentshotnewtop
SpicedSpices (218)

When using JSON objects, you can use the function Object.keys() to turn the keys into an array. After that, you can use just loop through the array. Here's my code

let values = {
  x: 1,
  y: 2,
  z: 5
}
console.log(values);

let array = Object.keys(values);

for(var i in values){
  array.push(values[i]);
}
console.log(array);

Hope this helped :)

Coding1000 (9)

@SpicedSpices Thank you, also how do you do it inside a class and the class objects will be looped in an array