convert object to object array javascript code example
Example 1: javascript object to array
const numbers = {
one: 1,
two: 2,
};
console.log(Object.values(numbers));
console.log(Object.entries(numbers));
Example 2: convert object to array javascript
var object = {'Apple':1,'Banana':8,'Pineapple':null};
var k = Object.keys(object);
var v = Object.values(object);
Example 3: javascript convert array to object
function arrayToObject(arr) {
var obj = {};
for (var i = 0; i < arr.length; ++i){
obj[i] = arr[i];
}
return obj;
}
var colors=["red","blue","green"];
var colorsObj=arrayToObject(colors);