简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM