0

I have an object which has multiple properties and values. How do I fetch all at once?

In this below code the products have multiple values. I want to grab all the prices and want the sum of them using java script

The code as follows.

var products = [
    {name:"anti-glair", price:100, quantity:200, status:"available"},
    {name:"Lens", price:300, quantity:35, status:"Un-available"},
    {name:"Optics", price:150, quantity:500, status:"available"}
    ];

I am trying like this

console.log(products.price);

But it is showing as not defined.

I want the all products sum as well.

Csaba Toth
  • 10,021
  • 5
  • 75
  • 121
Hemanth Paluri
  • 363
  • 3
  • 5
  • 12

5 Answers5

0

products refers to an array. You can access the first object in that array using index 0, the second using index 1, etc. Or you can loop through the array using a for loop, products.forEach, or a number of other ways outlined in the answers to this question.

Once you have the object, you can access its properties using the syntax you've shown.

So for instance:

var products = [
    {name:"anti-glair", price:100, quantity:200, status:"available"},
    {name:"Lens", price:300, quantity:35, status:"Un-available"},
    {name:"Optics", price:150, quantity:500, status:"available"}
    ];
console.log(products[0].price);
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Use higher order functions. To get an array of prices,

let prices = products.map(x => x.price)

Then to sum that,

let totalCost = prices.reduce((x, y) => x + y)
Fengyang Wang
  • 11,901
  • 2
  • 38
  • 67
0

Try this, this is what you want.

var products = [
    {name:"anti-glair", price:100, quantity:200, status:"available"},
    {name:"Lens", price:300, quantity:35, status:"Un-available"},
    {name:"Optics", price:150, quantity:500, status:"available"}
    ];

var TotalPrice = 0;
var obj = {};
for(var i = 0; i< products.length; i++){
  TotalPrice = TotalPrice + products[i].price;
  obj[i] = { price : products[i].price};
}
obj.total_price = TotalPrice;

console.log(obj);
M.Tanzil
  • 1,987
  • 1
  • 12
  • 28
0

For iterating and getting sum or price use Array#reduce method

var sum = products.reduce(function(a, b) {
  // calculate the price and add with total
  // multiply with `quantity` property if you want
  return a + b.price;
  // set initial value as 0
}, 0);

var products = [{
  name: "anti-glair",
  price: 100,
  quantity: 200,
  status: "available"
}, {
  name: "Lens",
  price: 300,
  quantity: 35,
  status: "Un-available"
}, {
  name: "Optics",
  price: 150,
  quantity: 500,
  status: "available"
}];

var sum = products.reduce(function(a, b) {
  return a + b.price;
}, 0);

console.log(sum)
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

Get first the length of the object before iterating the rest of the data:

var products = [
    {name:"anti-glair", price:100, quantity:200, status:"available"},
    {name:"Lens", price:300, quantity:35, status:"Un-available"},
    {name:"Optics", price:150, quantity:500, status:"available"}
    ];

var sumPrices = 0;
for(i = 0; i < products.length; i++) {
  console.log("Name: " + products[i].name + ", Price: " + products[i].price + ", Quantity: " + products[i].quantity[i] + ", Status: " + products[i].status);  
  sumPrices = sumPrices + products[i].price; 
}
console.log("Total sum of prices: " + sumPrices);
L. Herrera
  • 490
  • 4
  • 13