4

Let's say I have an array like this:

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

How do I concatenate this two-dimensional array so I get this as a result:

['ab', 'cd']

2 Answers 2

5

Just use Array.prototype.map().

var newArray = myArray.map(function (item) {return item.join('');}); //["ab", "cd"]
Sign up to request clarification or add additional context in comments.

1 Comment

I have never used the Array.prototype.map() function myself. Thank you very much!
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.