2

let arr = [[{x: 1},{y: 2}],[{z: 3}]]

let objs = []
arr.forEach(innerArray => {
  innerArray.forEach(obj => {
    objs.push(obj)
  })
})

console.log(objs) // [{x:1}, {y:2},{z:3}]

I don't want to map array two time, could you please help me to find the better way of doing this.

adiga
  • 34,372
  • 9
  • 61
  • 83
  • Is the objective flattenning the array? – VLAZ May 08 '19 at 11:17
  • Time complexity on this is pretty much fixed, but if speed is the issue, using as much of the browsers built in functions like `Array.flat`, would be the best option. – Keith May 08 '19 at 11:20
  • 1
    Possible duplicate of [Merge/flatten an array of arrays](https://stackoverflow.com/questions/10865025/merge-flatten-an-array-of-arrays) – pwolaq May 08 '19 at 11:24

1 Answers1

7

You can use Array.prototype.flat()

Note: All the methods below are just alternative which will have high speed. Your solution has O(n) time-complexity and all the method below have same time-complexity. The time-complexity of merging two arrays is O(n) not O(1)

let arr = [[{x: 1},{y: 2}],[{z: 3}]]
let objs = arr.flat();
console.log(objs)

Or another way is using concat() and reduce()

let arr = [[{x: 1},{y: 2}],[{z: 3}]]
let objs = arr.reduce((ac,a) => ac.concat(a),[])
console.log(objs)

A better idea suggested in comments is using apply()

let arr = [[{x: 1},{y: 2}],[{z: 3}]]
let objs = [].concat.apply([], arr)
console.log(objs)
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • Nice answer! I will even delete mine haha – DontVoteMeDown May 08 '19 at 11:21
  • how about: `[].concat.apply([], arr)` – pwolaq May 08 '19 at 11:22
  • Might be worth pointing out this doesn't change the time complexity. As none of the above change that!!. But then again, maybe the OP just meant the fastest.. :) – Keith May 08 '19 at 11:23
  • @Keith I don't know about `flat()`. But I think the `reduce()` and `concat()` method changed the complexity from `O(n*m)` to `O(n)` because in original solution OP is pushing each element but in `concat()` I don't think the whole array is iterated again. – Maheer Ali May 08 '19 at 11:26
  • 3
    @MaheerAli even the original solution is `O(n)`. Or `O(m*n)` depending on how you look at it. You only go over each array element *once* and over each object *once*. So in total, you have a single run over each object - `O(n)` although if you want to consider the run over the top level array, it's `O(m*n)`. `.flat()` will have the same performance characteristic, as it needs to essentially do the same operation - run over each element and pull each element of each sub-array into one. It might be a bit faster, as it can be optimised better. It's unlikely to have a different time complexity. – VLAZ May 08 '19 at 11:33
  • Basically what @VLAZ said, however you look at any of the above, it's either your code, or the JS engines code that has to loop & push each element into an array. A better time complexity example would be a linear find & a binary chop. – Keith May 08 '19 at 11:42