'How to do the multiplication with array objects
How to do the multiplication with array objects and get the all 3 outputs
const size = [
{ hight: 20, width: 23, length: 54 },
{ hight: 40, width: 43, length: 44 },
{ hight: 20, width: 23, length: 54 },
];
const total = (size) => {
const totalvalue = size.hight * size.width * size.length;
};
console.log(total)
Solution 1:[1]
You can use Array.prototype.map.
const size = [
{ hight: 20, width: 23, length: 54 },
{ hight: 40, width: 43, length: 44 },
{ hight: 20, width: 23, length: 54 },
];
const total = (size) => {
const totalvalue = size.hight * size.width * size.length;
return totalvalue;
};
console.log(size.map(total))
Solution 2:[2]
You'll have to loop through the array. Something like:
const size = [
{ hight: 20, width: 23, length: 54 },
{ hight: 40, width: 43, length: 44 },
{ hight: 20, width: 23, length: 54 },
];
const total = row => row.total = row.hight + row.width + row.length;
size.forEach(total);
console.log(size);
Solution 3:[3]
You can make use of the reduce function
var sum = array.reduce(function(pv, cv) { return pv + cv; }, 0);
Link to follow https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andy |
| Solution 2 | KooiInc |
| Solution 3 | Nick Parsons |
