NinjaTS
NinjaTS

Reputation: 89

how to multiply value of each object present in an array with value of another array?

I am looking for some solution to a problem that I faced during completing an assignment. The problem is similar to this that I have tried explaining below.

var arrOfObj = [{a:10 },{a:20},{a:30}, ......]
var arrToMultiply = [2,4,6, .....]

Result I am expecting

const result = [{a:10,result:20},{a:20,result:80},{a:30,result:180}, .....]

how can I multiply each value of array with the value of integer at same index inside object of the array?

Upvotes: 0

Views: 838

Answers (1)

NeNaD
NeNaD

Reputation: 20354

You can do it like this:

let arrOfObj = [{a: 10}, {a: 20}, {a: 30}];
let arrToMultiply = [2, 4, 6];

let result = arrOfObj.map((item, index)=> ({...item, result: item.a*arrToMultiply[index]}));
console.log(result);

Upvotes: 1

Related Questions