array of objects js code example
Example 1: list javascript
var fruits = ["Apple", "Banana", "Cherry"];
var books = [];
console.log(fruits[0])
fruits.push("Orange")
fruits[1]="Blueberry"
fruits.splice(0, 1)
fruits.splice(0, 2)
Example 2: array of objects javascript
var widgetTemplats = [
{
name: 'compass',
LocX: 35,
LocY: 312
},
{
name: 'another',
LocX: 52,
LocY: 32
}
]
Example 3: 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 4: what are the two ways to create an array in javascript examples?
let scores = Array(10);
Example 5: js objects in array
let car = cars.find(car => car.color === "red");