[英]How do I map an array of numbers to a property in an array of objects?
I am trying to map an array of values to a property in an array of objects. 我正在尝试将值数组映射到对象数组中的属性。
var myArray = ['2', '4', '7'];
var myProducts = [
{id: '755', price: '10'},
{id: '756', price: '20'},
{id: '757', price: '30'}
];
Please note that the length of myArray
will always equal the number of objects in myProducts
. 请注意,
myArray
的长度将始终等于myProducts
中对象的数量。
My first thought was to loop through objects in myProducts
, and then mapping myArray
to the shipping
property inside that loop: 我的第一个想法是遍历
myProducts
对象,然后将myArray
映射到该循环内的shipping
属性:
myProducts.forEach(function(obj) {
obj.shipping = Array.prototype.map(myArray);
});
But this isn't working, and I am also now questioning whether I should be using .map
within the forEach
loop. 但这是行不通的,而且我现在也在质疑是否应该在
forEach
循环中使用.map
。 What's the best way to do this? 最好的方法是什么?
Desired result : 所需结果 :
var myProducts = [
{id: '755', price: '10', shipping: '2'},
{id: '756', price: '20', shipping: '4'},
{id: '757', price: '30', shipping: '7'}
];
You could take the index and assign the value to the property 您可以获取索引并将值分配给属性
var myArray = ['2', '4', '7'], myProducts = [{ id: '755', price: '10' }, { id: '756', price: '20' }, { id: '757', price: '30' }]; myProducts.forEach((o, i) => o.shipping = myArray[i]); console.log(myProducts);
.as-console-wrapper { max-height: 100% !important; top: 0; }
One way is to use the second argument of .forEach(
's callback, which gives you the index of the iterator: 一种方法是使用
.forEach(
的回调函数的第二个参数,它为您提供迭代器的索引:
var myArray = ["2", "4", "7"]; var myProducts = [ { id: "755", price: "10" }, { id: "756", price: "20" }, { id: "757", price: "30" } ]; myProducts.forEach(function(obj, index) { obj.shipping = myArray[index]; }); console.log(myProducts);
Just use a regular for
loop, because you need to keep track of the index. 只需使用常规的
for
循环,因为您需要跟踪索引。
var myArray = ['2', '4', '7']; var myProducts = [ {id: '755', price: '10'}, {id: '756', price: '20'}, {id: '757', price: '30'} ]; for (var i = 0; i < myProducts.length; i++) { myProducts[i].shipping = myArray[i]; } console.log(myProducts);
It depends on whether you want to mutate that object specifically or if you don't mind creating a new object 这取决于您是要特定地更改该对象还是不介意创建一个新对象
Creating a new object 创建一个新对象
newProducts = myProducts.map((product, index ) => {
product['shipping'] = myArray[index];
return product;
})
Mutating existing object 突变现有对象
myArray.forEach((item, index) => {
myProducts[index]['shipping'] = item;
});
Creating a new item is useful if you are doing this in a function and don't want to mangle the products array in its original location before you called this since objects are passed by reference. 如果您要在函数中执行此操作并且不想在调用此函数之前将product数组摆在其原始位置,则创建新项目很有用,因为对象是通过引用传递的。
Use index based for each loop iterator
var myArray = ['2', '4', '7'];
var myProducts = [
{ id: '755', price: '10' },
{ id: '756', price: '20' },
{ id: '757', price: '30' },
];
myProducts.forEach(function(obj, index) {
obj.shipping = myArray[index];
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.