-1

I have an array like following:

[
  [],
  [],
  [],
  [ { count: 16, date: '2020-11-13' } ],
  [ { count: 25, date: '2020-11-12' } ],
]

and I need to convert it to the following format:

[
 { count: 16, date: '2020-11-13' },
 { count: 25, date: '2020-11-12' }
]

What's the most efficient way to do this?

Khadar111
  • 151
  • 2
  • 3
  • 12

2 Answers2

-1

You can use the flat method.

let i = [
          [],
           [],
           [],
           [ { count: 16, date: '2020-11-13' } ],
           [ { count: 25, date: '2020-11-12' } ],
        ]
i.flat() // this will give the desired output
-1
var arrObj=[];
arr.map(a => {
    if (a.length > 0){
        arrObj.push(a[0]);
    } 
    return 
});

console.log(arrObj)
  • 1
    Please don't post code-only answers. For future readers, it's far more interesting to see explained why this answers the question. And answers with explanations tend to have better acceptance. – Akif Nov 16 '20 at 11:27
  • @Akif Your comment is correct, but I see you reviewed this as "Recommend Deletion", which you shouldn't have. Deletion is only for answers that aren't even an attempt to answer the question, like "thanks", "me too", etc. If an answer is low quality but still an attempt to answer the question, downvote and/or comment, but don't recommend deletion. See [this post](https://meta.stackoverflow.com/q/287563/4284627) by a moderator. – Donald Duck Nov 16 '20 at 11:56