convert object key into array code example

Example 1: convert object to array javascript

var object = {'Apple':1,'Banana':8,'Pineapple':null};
//convert object keys to array
var k = Object.keys(object);
//convert object values to array
var v = Object.values(object);

Example 2: assign an element value as key in array of objects

var keys = ['HP', 'QP', 'PS'],
    object = Object.assign(...keys.map(key => ({ [key]: 0 })));
    
console.log(object);

Example 3: how to convert object to array in javascript

const propertyValues = Object.values(person);

console.log(propertyValues);

Example 4: js array to object with keys

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

Tags:

Java Example