简体   繁体   English

循环遍历数组的每个 object 和 append 每个 object 从第二个数组到第一个数组

[英]Loop through each object of an array and append each object from second array to the first array

I have two arrays of objects.我有两个 arrays 对象。 I need to loop through the first array and append each object of the second array to each object of the first array.我需要遍历第一个数组和 append 第二个数组的每个 object 到第一个数组的每个 object 。

First array:第一个数组:

const productsArray = [
  {Name: 'product1', SKU: 'sku1'},
  {Name: 'product2', SKU: 'sku2'}, 
  {Name: 'product3', SKU: 'sku3'}
];

Second Array:第二个数组:

const quantitiesArray = [
  {Quantity: 100},
  {Quantity: 300}, 
  {Quantity: 600}
];

Desired Outcome:期望的结果:

const newProductsArray = [
  {Name: 'product1', SKU: 'sku1', Quantity: 100},
  {Name: 'product2', SKU: 'sku2', Quantity: 300}, 
  {Name: 'product3', SKU: 'sku3', Quantity: 600}
]

There will always be the same amount of objects in each array.每个数组中总是有相同数量的对象。

This should point you in the right direction.这应该为您指明正确的方向。 A loop plus Object.assign() I believe is what you're looking for.一个循环加上 Object.assign() 我相信这就是你要找的。

 const productsArray = [{ Name: 'product1', SKU: 'sku1' }, { Name: 'product2', SKU: 'sku2' }, { Name: 'product3', SKU: 'sku3' } ]; const quantitiesArray = [{ Quantity: 100 }, { Quantity: 300 }, { Quantity: 600 } ]; function appendArray() { let assignedArray; for (let i = 0; i < productsArray.length; i++) { assignedArray = Object.assign(productsArray[i], quantitiesArray[i]) } return assignedArray; } console.log(appendArray())

 const productsArray = [ {Name: 'product1', SKU: 'sku1'}, {Name: 'product2', SKU: 'sku2'}, {Name: 'product3', SKU: 'sku3'} ], quantitiesArray = [ {Quantity: 100}, {Quantity: 300}, {Quantity: 600} ]; const res=quantitiesArray.map((q,i)=>( {...productsArray[i],...q} )) console.log(res);

 const productsArray = [ {Name: 'product1', SKU: 'sku1'}, {Name: 'product2', SKU: 'sku2'}, {Name: 'product3', SKU: 'sku3'} ]; const quantitiesArray = [ {Name: 'product1', SKU: 'sku1', Quantity: 100}, {Name: 'product2', SKU: 'sku2', Quantity: 300}, {Name: 'product3', SKU: 'sku3', Quantity: 600} ]; const newProductsArray = []; productsArray.forEach((item,index) => { newProductsArray.push({...item,...quantitiesArray[index]}); }); console.log(newProductsArray);

Since the result is a completely new array, you can use Array#map - which creates a new array, and the spread operator as follows:由于结果是一个全新的数组,您可以使用Array#map - 它创建一个新数组,并使用扩展运算符如下:

 const productsArray = [ {Name: 'product1', SKU: 'sku1'}, {Name: 'product2', SKU: 'sku2'}, {Name: 'product3', SKU: 'sku3'} ]; const quantitiesArray = [ {Quantity: 100}, {Quantity: 300}, {Quantity: 600} ]; const newProductsArray = productsArray.map( (prodArr,i) => ({...prodArr, ...quantitiesArray[i]}) ) console.log( newProductsArray );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 遍历数组,将每个项目添加到对象中,然后将其推入Javascript中的数组 - Loop through an Array, add each item to an object and push it to an array in Javascript 遍历数组 - 将从 promise 返回的值添加到数组中的每个 object - loop through array - add value returned from promise to each object in array 将属性附加到数组中的每个对象 - Append property to each object within array 如何将对象附加到数组中的每个项目 - How to append an object to each item in an array 在each()中遍历数组 - loop through array in each() Angular NgFor遍历数组对象,每个项目都有延迟 - Angular NgFor Loop Through Array Object with Delay for Each Item 循环遍历 json 数组并使用 javascript 添加到每个对象 - loop through json array and add to each object using javascript 遍历对象数组并将每个元素插入html - Loop through object array and insert each element into html 为每个循环从中返回数组对象并将其实现到新循环 - Returning an array object from a for each loop and impleting it to a new loop 我如何循环遍历数组中每个 object 的属性,每个 object 的属性都不同。 这是 Javascript - How do i loop through the properties of each object in the array, the properties are different in each object. This is Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM