javascript how to put keys in a object into array code example

Example 1: convert object to array javascript

const numbers = {
  one: 1,
};

const objectArray = Object.entries(numbers);

objectArray.forEach(([key, value]) => {
  console.log(key); // 'one'
  console.log(value); // 1
});

Example 2: js array to object with keys

const arr = ['a','b','c'];
const res = arr.reduce((a,b)=> (a[b]='',a),{});
console.log(res)

Example 3: js convert obj to array

const array = [
  ['key', 1],
  ['two', 2],
];

Object.fromEntries(array);
// { one: 1, two: 2 }