0

I want to push all array of objects into a single array.

For example below is a transaction array which has two different arrays:

transaction:

0:(3) [{…}, {…}, {…}]
1:(2) [{…}, {…}]

I want something like this:

transaction:

0:(5) [{…}, {…}, {…},{…}, {…}]

I am pushing arrays inside transaction array by using a for loop, and i don't how to use concat operation inside for loop.

This below is my for loop i am push objects into transaction array:

for (var j = 0; j < $scope.allRecent.length; j++)
{
    if (uniqueDates[i].dates == $scope.formatDate($scope.allRecent[j].date))
    {
        tempObj.transaction.push($scope.allRecent[j].transaction)
    }
}
James Z
  • 12,209
  • 10
  • 24
  • 44

3 Answers3

0

To push "abc" to subArray1 in this example: array = [[subArray1], [subArray2]], use array[0].push("abc") as subArray1 is at index 0 of array

0

You can simply concat all the arrays. Try the following:

var array = [[{a:1},{b:1},{c:1}], [{a:2},{b:1}]];

var result = [];

for(var i = 0; i < array.length; i++){
    result = result.concat(array[i]);
}

console.log(result);
amrender singh
  • 7,949
  • 3
  • 22
  • 28
0

You can use Array.reduce() along with the spread operator

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

const array1 = [1,2,3,4];
const array2 = [5,6,7,8];
const array3 = [9,10];
const arr = [array1, array2, array3]
console.log(arr);

const reducer = (accumulator, currentValue) => [...accumulator, ...currentValue];
console.log(arr.reduce(reducer));
DavidZ
  • 441
  • 2
  • 6