0

I think this is silly question and simple but I can't find any result in google and other resource

I have array of object like this

 const myArr = [
 [{
 id: 1,
 price: 200,
 }, {
 id: 2,
 price: 900,
 }, {
 id: 3,
 price: 100,
 }],
 [{
 id: 5,
 price: 100,
 }]
 ];

In other word I have an array and my array contain some array and each of inner array contain some object inside them

 arr [ [ {},{},{} ] , [ {} ] ]

now I want get two thing

  1. count of all products ?
  2. sum of all products ?

*(each object = one product)

Narendra Jadhav
10.3k15 gold badges35 silver badges45 bronze badges
asked May 8, 2018 at 14:49
1
  • Could you show the code you have tried, please? Commented May 8, 2018 at 14:50

4 Answers 4

4

Flatten to a single array by spreading into Array.concat().

Use Array.reduce() to get the sum.

The count is flattened array's length.

const myArr = [[{"id":1,"price":200},{"id":2,"price":900},{"id":3,"price":100}],[{"id":5,"price":100}]];
const flattened = [].concat(...myArr);
const count = flattened.length;
const sum = flattened.reduce((s, o) => s + o.price, 0);
console.log('count', count);
console.log('sum', sum);

answered May 8, 2018 at 14:52
Sign up to request clarification or add additional context in comments.

6 Comments

@OriDrori, we have same ideas but you was faster. Anyway, thumbs up and upvote.
@OriDrori my friend , now i have another question about this , you think every product have property { count : n } and this means the count of each products now how can i get sum price ??
@OriDrori , for example => [ [ { id:1 , price:100 , count:10 } ] , [ { id:2 , price:200 , count:5 } ] ]; i want this result (price of product)100 * (count of product)10 . .......
You actually have the answer. Look at the sum calculation in my code...
can u give me more details
|
2

You can use spread to flat the array:

var myArr = [
 [
 { 
 id:1,
 price:200,
 },
 { 
 id:2,
 price:900,
 },
 { 
 id:3,
 price:100,
 }
 ],
[
 { 
 id:5,
 price:100,
 }
]
];
var arr = [].concat(...myArr);
console.log('Length: ', arr.length);
var sum = arr.reduce((m, o) => m + o.price, 0);
console.log('Sum: ', sum);

answered May 8, 2018 at 14:54

Comments

2

You can use concat to wrap all objects within one single array.

Apply length property in order to find out the number of objects in the array and then reduce method to get the sum.

const myArr = [ [ { id:1, price:200, }, { id:2, price:900, }, { id:3, price:100, } ], [ { id:5, price:100, } ] ];
arr = myArr.reduce((acc, arr) => acc.concat(arr), []);
sum = arr.reduce((acc, item) => acc + item.price, 0);
console.log('count ' + arr.length);
console.log('sum ' + sum)

answered May 8, 2018 at 14:54

Comments

2

ES6

You can also use reduce method of array to get the required result

reduce can be used to iterate through the array, adding the current element value to the sum of the previous element values.

DEMO

const myArr = [[{id: 1,price: 200,}, {id: 2,price: 900,}, {id: 3,price: 100,}],[{id: 5,price: 100,}]];
 
let result = myArr.reduce((r,v)=>{
 r.count += v.length;
 r.sum += v.reduce((total,{price}) => total+price,0);
 return r;
},{count:0,sum:0}) 
console.log(result);
.as-console-wrapper {max-height: 100% !important;top: 0;}

answered May 8, 2018 at 15:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.