0

I have two arrays of objects. I need to loop through the first array and append each object of the second array to each object of the first array.

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.

2
  • 3
    What have you tried? Where are you getting stuck? Do you know about Object.assign()? Commented Aug 1, 2022 at 19:01
  • Please share what research or attempts you have already done. Solution: There are multiple ways this can be achieved but hint for one of the possible solutions: Just run a simple for loop on one of your arrays and copy items at given index from both arrays into one of your arrays. Use something like this to set your value at given index { ...productsArray[i], ...quantitiesArray[i] }; P.S.: Please check out JavaScript Spread syntax (...) to understand more about ...object usage. Here Commented Aug 1, 2022 at 19:11

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

This should point you in the right direction. A loop plus Object.assign() I believe is what you're looking for.

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

Comments

0

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

Comments

0

Since the result is a completely new array, you can use Array#map - which creates a new array, and the spread operator as follows:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.