簡體   English   中英

javascript - 連接二維數組

[英]javascript - Concatenate a two dimensional array

假設我有一個這樣的數組:

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

我如何連接這個二維數組,所以我得到這個結果:

['ab', 'cd']

只需使用Array.prototype.map()

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM