-1

I am having a difficult time, there is some bad mapping going on on my code.

I have an array containing array of objects like that :

[
   [{a: 1, b: 2},{a: 1, b: 3} ],
   [{a: 5, b: 2},{a: 2, b: 5}]
]

And I want to make like that :

[
   {a: 1, b: 2},
   {a: 1, b: 3},
   {a: 5, b: 2},
   {a: 2, b: 5}
]

In order to do that, I thought I found the magical solution, make things flat, using flatten function, it was not working ( this problem is just a piece of code in a lot of code ) and I was wondering why, i wasted some time to find that this the problem, it is not the behovior I am expecting, as you can see in the image, the first thing I have is an array containing an array having two objects, with flatten method, I was expecting an array of two objects, but I am getting what you see in the image :

enter image description here

The code I have ttried is this :

const expectedArray = R.flatten(myArrayOfArraysOfObjects);

Full example :

const singleTronconPoints = troncon => {
  return troncon.geometri_linestring;
};
console.log('troncons : ');
console.log(troncons);
console.log('map troncons points');
console.log(map(singleTronconPoints, troncons));
console.log('flatten');
console.log(flatten(map(singleTronconPoints, troncons)));

and this is full result : enter image description here

How can I solve that, is there another magical ( :P ) solution ( method ) to solve the problem ?

Any help would be much appreciated.

TaouBen
  • 1,165
  • 1
  • 15
  • 41
  • 2
    Please add a [mcve] that shows the actual problem, because your [script parts work](https://ramdajs.com/repl/#?const%20input%20%3D%20%5B%0A%20%20%20%5B%7Ba%3A%201%2C%20b%3A%202%7D%2C%7Ba%3A%201%2C%20b%3A%203%7D%20%5D%2C%0A%20%20%20%5B%7Ba%3A%205%2C%20b%3A%202%7D%2C%7Ba%3A%202%2C%20b%3A%205%7D%5D%0A%5D%3B%0Aconst%20output%20%3D%20R.flatten%28input%29%3B%0Aconsole.log%28JSON.stringify%28output%29%29%3B) – Andreas Jul 17 '20 at 13:36
  • then it must be not the flat method the problem, it must something else, because ramda flatten method is using flat already – TaouBen Jul 17 '20 at 13:50
  • Does this answer your question? [Merge/flatten an array of arrays](https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays) – customcommander Jul 20 '20 at 08:04

4 Answers4

3

Array.prototype.reduce() can also be an option:

const arr =[
  [{a: 1, b: 2},{a: 1, b: 3}],
  [{a: 5, b: 2},{a: 2, b: 5}]
]

const expectedArray = arr.reduce((acc, array) => {
  acc.push(...array);
  return acc;
}, []);
soltex
  • 2,993
  • 1
  • 18
  • 29
2

You can use array.flat

let a = [
  [{
    a: 1,
    b: 2
  }, {
    a: 1,
    b: 3
  }],
  [{
    a: 5,
    b: 2
  }, {
    a: 2,
    b: 5
  }]
];

let b = a.flat();
console.log(b)

Alternatively you can use reduce and inside callback use forEach and puch items from the nested array to accumulator array

let a = [
  [{
    a: 1,
    b: 2
  }, {
    a: 1,
    b: 3
  }],
  [{
    a: 5,
    b: 2
  }, {
    a: 2,
    b: 5
  }]
];

let b = a.reduce((acc, curr) => {
  curr.forEach(item => acc.push(item))
  return acc;
}, []);

console.log(b)
brk
  • 48,835
  • 10
  • 56
  • 78
2

use reduce() + push which faster flat() method.

refer this for to check performance. : https://jsbench.me/0ikcqa83ck/1

let arr = [
           [{a: 1, b: 2},{a: 1, b: 3} ],
           [{a: 5, b: 2},{a: 2, b: 5}]
        ]

      console.log(arr.flat())
        
        let flattenArr = arr.reduce((acc, val) => (acc.push(...val),acc), [])
        console.log(flattenArr);
Vijay Palaskar
  • 526
  • 5
  • 15
1

Array.flat is the magical solution you are looking for !

var arr = [
   [{a: 1, b: 2},{a: 1, b: 3} ],
   [{a: 5, b: 2},{a: 2, b: 5}]
]

console.log(arr.flat())
Harmandeep Singh Kalsi
  • 3,315
  • 2
  • 14
  • 26