Vinicius Martins
Vinicius Martins

Reputation: 83

Merge Array of Arrays | NodeJS

I would like to know how could I merge an array of arrays. For example, I have the following array:

[ [ {marca: 'fiat', modelo: 'palio'} ], [ {marca: 'nissan', modelo: 'march'} ] ]

I'd like to get this array, after merging the array above:

[ {marca: 'fiat', modelo: 'palio'}, {marca: 'nissan', modelo: 'march'} ]

Thanks, guys, I appreciate it.

Upvotes: 1

Views: 719

Answers (7)

Shidersz
Shidersz

Reputation: 17190

You can give a try to the new, but still experimental, flat() method of arrays...

const input = [
  [ {marca: 'fiat', modelo: 'palio'} ],
  [ {marca: 'nissan', modelo: 'march'} ]
];

console.log(input.flat());

Or alternatively use reduce() like this:

const input = [
  [ {marca: 'fiat', modelo: 'palio'}, {marca: 'fiat', modelo: 'fiesta'} ],
  [ {marca: 'nissan', modelo: 'march'} ]
];

let res = input.reduce((acc, curr) => acc.push(...curr) && acc, []);

console.log(res);

Upvotes: 2

ZER0
ZER0

Reputation: 25322

Where implemented, you can use the standard Array's flat method:

let arr = [ [ {marca: 'fiat', modelo: 'palio'} ], [ {marca: 'nissan', modelo: 'march'} ] ];

console.log(arr.flat());

As alternative, if it's just a shallow flat, you can obtain the same result with concat and spread operator:

console.log([].concat(...arr));

Upvotes: 0

Vinicius Martins
Vinicius Martins

Reputation: 83

I used lodash's flattenDeep method to solve it. For example:

_.flattenDeep(Array)

Thank you all for the answers.

Upvotes: 0

Jeremy Ruffell
Jeremy Ruffell

Reputation: 45

There are a few ways of doing this, I would recommend Reduce, as there is no need to be installing npm packages when not needed.

let arrays = [
  [{
    marca: 'fiat',
    modelo: 'palio'
  }],
  [{
    marca: 'nissan',
    modelo: 'march'
  }]
]

arrays = arrays.reduce((a, b) => a.concat(b), []);

console.log(arrays);

Upvotes: 0

Kenneth Obando
Kenneth Obando

Reputation: 83

For the record, if you are using NodeJs, there is a package named array-flatten, you can install it with

npm install array-flatten --save

For example:

var flatten = require('array-flatten')

flatten([1, [2, [3, [4, [5], 6], 7], 8], 9])
//=> [1, 2, 3, 4, 5, 6, 7, 8, 9]

https://www.npmjs.com/package/array-flatten

Upvotes: 0

eol
eol

Reputation: 24555

You can use the ES6-spread operator in combination with Array.reduce, e.g.

const arr = [ [ {marca: 'fiat', modelo: 'palio'} ], [ {marca: 'nissan', modelo: 'march'} ] ];
console.log(arr.reduce((a, b) => [...a, ...b], [])); 

Upvotes: 2

Dacre Denny
Dacre Denny

Reputation: 30360

There are a number of ways this can be done. Assuming your input data is such that each item of the input array is an array itself with one item, the use of Array#map would be sufficient:

const input = [
  [{
    marca: 'fiat',
    modelo: 'palio'
  }],
  [{
    marca: 'nissan',
    modelo: 'march'
  }]
];

/* 
For each subArray of input array where the subArray
is known to have a single element, extract that single
element and return it as the result of each map iteration 
*/
const output = input.map(([item]) => item)

console.log(output)

Alternatively, for a more general solution that "flattens" an array of array items that have variable length, you could achieve that via Array#reduce as follows:

const input = [
  [{
    marca: 'fiat',
    modelo: 'palio'
  }, {
    test: 'foo'
  }],
  [{
    marca: 'nissan',
    modelo: 'march'
  }]
];

const output = input.reduce((finalArray, subArray) => {
  
  /* 
  For each item being iterated during reduce, concat
  the subArray to the finalArray 
  */
  return finalArray.concat(subArray);

}, [])

console.log(output)

Upvotes: 0

Related Questions