0

is there a way to merge the following array :

var arr = [["2014-2-5", "2014-2-4", "2014-1-9", "2014-1-8"], [], ["2014-2-4"], [], []]

and make it look like :

["2014-2-5", "2014-2-4", "2014-1-9", "2014-1-8", "2014-2-4"]

I tried console.log($.merge(arr )); but it is not working.

Thanks

OussamaLord
  • 997
  • 4
  • 25
  • 39

2 Answers2

0

Use Array's reduce in combination with concat:

arr.reduce(function(previousValue, currentValue, index, array){
  return previousValue.concat(currentValue);
});
bits
  • 7,480
  • 7
  • 43
  • 54
0

Not merge but flatten-

Array.prototype.flatten= function(){
    var A= [];
    this.forEach(function(itm){
        if(itm!= undefined){
            if(!itm.flatten) A.push(itm);
            else A= A.concat(itm.flatten());
        }
    });
    return A;
}

var arr= [["2014-2-5", "2014-2-4", "2014-1-9", 
"2014-1-8"], [], ["2014-2-4"], [], []];
arr.flatten();

// returned value: (Array)
['2014-2-5', '2014-2-4', '2014-1-9', '2014-1-8', '2014-2-4']
kennebec
  • 94,076
  • 30
  • 99
  • 125