Javascript - merge a two-dimensional array

Say I have an array like this:

var myArray = [
                  ['a', 'b'],
                  ['c', 'd']
              ]

How to combine this two-dimensional array, so I get this as a result:

['ab', 'cd']
+4
source share
2 answers

Just use it Array.prototype.map().

var newArray = myArray.map(function (item) {return item.join('');}); //["ab", "cd"]
+5
source

http://jsfiddle.net/6bw65xj4/1/

var tmp = '',
    newArray = [],
    myArray = [
        ['a', 'b'],
        ['c', 'd']
    ];

for(var i=0; i<myArray.length; i++){    
    for(var j=0; j<myArray[i].length; j++) {
       tmp = tmp + myArray[i][j];             
    }
    newArray.push(tmp);
    tmp = ''
}

console.log(newArray);
0
source

Source: https://habr.com/ru/post/1607268/


All Articles