简体   繁体   English

如何将数组中存在的每个 object 的值与另一个数组的值相乘?

[英]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.

相关问题 如何将坐标数组中的每个值相乘 - How to multiply each value in an array of coordinates 如果对象包含值存在于另一个数组中的属性,则过滤对象数组 - Filter array of objects if object contains attribute with value present in another array 根据值从一个数组中删除对象(如果存在于另一个数组中) - Removing object from one array if present in another array based on value 如何根据主 object 中存在的其他值,在另一个对象数组中存在的数组中添加 object - How to add an object inside an array present inside another array of objet on the basis of other value present inside the main object 将 object 转换为数组,并乘以 object 值 - Turning object into array, and the multiply it by object value 检查对象或数组中是否存在值 - Check if value is present in an object or array 检查对象数组中的每个 object 是否包含另一个数组中的值(JavaScript) - Check if each object in array of objects contains a value that is in another array (JavaScript) 如果不存在于另一个数组中,如何删除数组中的对象 - How to remove object in array if not present in another array 如何将值从数组中的对象添加到另一个数组中的另一个对象? - How to add value from object in an array into another object in another array? 如何将对象的每个值放入数组 - How to place each value of an object into an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM