[英]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?如何在数组的 object 内的相同索引处将数组的每个值与 integer 的值相乘?
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.