当前位置: 动力学知识库 > 问答 > 编程问答 >

angularjs - How to delete brackets from array, but keep quotes

问题描述:

I have an array, with even number of elements:

var peoples = [

["1", "Adam", "Jones"],

["2", "Michael", "Jordan"],

["3", "Frank", "Forman"],

["4", "John", "Bryant"],

["5", "James", "Johnson"],

["6", "Vincent", "Carter"],

["7", "George", "Williams"],

["8", "Brandon", "Clarkson"]

];

and I’m trying to merge arrays in pairs by following pattern:

["1", "Adam", "Jones", "2", "Michael", "Jordan"]

["3", "Frank", "Forman","4", "John", "Bryant"]

etc.

I have a problem with following code:

for (var i = 0; i < peoples.length / 2; i++) {

array1[i].push(array2[i].join(","))

}

which is generating that result:

["1","Adam","Jones","2,Michael,Jordan"]

and it should be:

["1","Adam","Jones","2","Michael","Jordan"]

Here is my jsfiddle https://jsfiddle.net/danny3b/k5hza694/

网友答案:

I've already done it by myself. I was looking for concat() method.

for (var i = 0; i < peoples.length / 2; i++) {
        array1[i] = array1[i].concat(array2[i])
    }

https://jsfiddle.net/danny3b/rfju9949/

网友答案:

What join does is concatenating all of the strings in the array. Instead of that, you should insert all of the elements in there.

分享给朋友:
您可能感兴趣的文章:
随机阅读: