Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine Array(s) into single object in JavaScript

Currently I have 3 arrays:

var idArray = [13, 24, 35];    
var dateArray = [20181920, 20181120, 20172505];
var contentArray = ["content1", "content2", "content3"];

I would like to merge all the array into one object instead and I tried using for loop but got an error instead:

var finalObj = {};

for (var y = 0; y < dateArray.length; y++) { 
    finalObj[y].id = idArray[y];
    finalObj[y].date =  dateArrayArrange[y];
    finalObj[y].content =  contentArray[y];

}

The end result I want to achieve:

finalObj = [{id:13, date:20181920, content:"content1"},
          {id:24, date:20181120, content:"content2"},
          {id:35, date:20172505, content:"content3"}];
like image 665
Vincent Chua Avatar asked Jul 28 '18 11:07

Vincent Chua


People also ask

How do I combine arrays of objects into one?

assign() method to convert an array of objects to a single object. This merges each object into a single resultant object. The Object. assign() method also merges the properties of one or more objects into a single object.

How do you combine two arrays into an array of objects?

To combine two arrays into an array of objects, use map() from JavaScript.

How can I merge two arrays in JavaScript?

The concat() method concatenates (joins) two or more arrays. The concat() method returns a new array, containing the joined arrays.

How do I combine two arrays?

In order to merge two arrays, we find its length and stored in fal and sal variable respectively. After that, we create a new integer array result which stores the sum of length of both arrays. Now, copy each elements of both arrays to the result array by using arraycopy() function.

How to merge two arrays in JavaScript?

Using the spread operator or the concat () method is the most optimal solution. If you are sure that all inputs to merge are arrays, use spread operator. In case you are unsure, use the concat () method. You can use the push () method to merge arrays when you want to change one of the input arrays to merge.

How do you add objects to an array in JavaScript?

We loop over the source array looking for new data, and for every object that is not yet found in our target array we simply add that object using target.push (sourceElement) If, based on our key property ('name'), an object is already in our target array - we update its properties and values using Object.assign (targetElement, sourceElement) .

How to merge arrays using concat and spread operator?

Using concat () Method: The concat () method accept arrays as arguments and returns the merged array. Using spread operator: Spread operator spreads the value of the array into its constituent elements.

What is concat () method in JavaScript?

As we have seen, the concat () method merged both the arrays and returns a new array with the result. In this example, we have taken three arrays and have merged them using the JavaScript concat () method. All the arrays have unique elements initially.


2 Answers

finalObj is an object you need to push the value in an array.Beside you can also use array map method which will return an array so there is no need to separately push the values to an array.

Use map method on any of the arrays and use the index to get the corresponding values from other array

var idArray = [13, 24, 35];
var dateArray = [20181920, 20181120, 20172505];
var contentArray = ["content1", "content2", "content3"];

let m = idArray.map(function(item, index) {
  // here the map method will push the object to an 
  // array and will return that array
  return {
    id: item,
    date: dateArray[index],
    content: contentArray[index]
  }

})
console.log(m)
like image 70
brk Avatar answered Sep 30 '22 08:09

brk


Your finalObj is an array of objects, so you must initialise it as an array, like this :

var finalObj = [];
var idArray = [13, 24, 35];
var dateArray = [20181920, 20181120, 20172505];
var contentArray = ["content1", "content2", "content3"];

for (var y = 0; y < dateArray.length; y++) {
  finalObj.push(
    { id: idArray[y], date: dateArray[y], content : contentArray[y] }
  )
}

for the "pushing to final array" part, your code inside the loop may work if you add an extra finalObj[y] = {} to initialise it and so you have :

for (var y = 0; y < dateArray.length; y++) {
  finalObj[y] = {}  
  finalObj[y].id = idArray[y];
  finalObj[y].date = dateArray[y];
  finalObj[y].content = contentArray[y];
}

an it works but

  finalObj.push(
    { id: idArray[y], date: dateArray[y], content : contentArray[y] }
  )

is shorter and still readable i think :)

like image 42
Jayffe Avatar answered Sep 30 '22 09:09

Jayffe