using Object.values() method to assign a constant array?

443 Views Asked by At

How would I fill const objArray with numObj object's values using the Object.values() method? I've only been able to do it with a for loop + push method (demonstrated below)

const numObj = {
  oddNum: 1,
  evenNum: 2,
  foundNum: 5,
  randomNum: 18
};

const objArray = [];

for (var values in numObj) {
  objArray.push(numObj[values]);
  }
2

There are 2 best solutions below

0
Nenad Vracar On

You could use spread syntax with push method.

const numObj = {
  oddNum: 1,
  evenNum: 2,
  foundNum: 5,
  randomNum: 18
};

const objArray = [];
objArray.push(...Object.values(numObj));
console.log(objArray)

2
Jonas Wilms On
 objArray.push(...Object.values(numObj));

Or you directly assign it:

const objArray = Object.values(numObj));