how to make an object an array in javascript code example
Example 1: javascript object toarray
var obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}
var result = Object.keys(obj).map(function(key) {
return [Number(key), obj[key]];
});
console.log(result);
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: how to convert object to array in javascript
const propertyValues = Object.values(person);
console.log(propertyValues);
Example 4: create array of objects javascript
let products = [
{
name: "chair",
inventory: 5,
unit_price: 45.99
},
{
name: "table",
inventory: 10,
unit_price: 123.75
},
{
name: "sofa",
inventory: 2,
unit_price: 399.50
}
];
function listProducts(prods) {
let product_names = [];
for (let i=0; i<prods.length; i+=1) {
product_names.push(prods[i].name);
}
return product_names;
}
console.log(listProducts(products));
function totalValue(prods) {
let inventory_value = 0;
for (let i=0; i<prods.length; i+=1) {
inventory_value += prods[i].inventory * prods[i].unit_price;
}
return inventory_value;
}
console.log(totalValue(products));
Example 5: js convert obj to array
const array = [
['key', 1],
['two', 2],
];
Object.fromEntries(array);