javascript - Using join method in nested array - Stack Overflow

i have this nested array arr: [[ "one", "two" , "three"]]I want to extra

i have this nested array arr:

[[ "one", "two" , "three"]] I want to extract the values and join them in a var called numbers and separate them by ";"

I used this method :

var itemsArray = arr.join(";");

what i a getting is this :

one,two,three

Although what i am aiming for is one;two;three

It's reading the separator.

i have this nested array arr:

[[ "one", "two" , "three"]] I want to extract the values and join them in a var called numbers and separate them by ";"

I used this method :

var itemsArray = arr.join(";");

what i a getting is this :

one,two,three

Although what i am aiming for is one;two;three

It's reading the separator.

Share Improve this question asked Oct 4, 2016 at 10:27 yasser hyasser h 6292 gold badges10 silver badges18 bronze badges 1
  • 2 Use arr[0].join(';');. The array is nested array. – Tushar Commented Oct 4, 2016 at 10:27
Add a ment  | 

3 Answers 3

Reset to default 4

if the array is nested and number of levels are only two, then try

var arr = [[ "one", "two" , "three"]];
var itemsArray = arr.map( function( item ){ return item.join( ";" )  } ).join(";");

console.log( itemsArray );

You could use a deep joining for nested arrays.

var array = ['zero', ['one', 'two' , 'three', ['four', ['five', 'six', ['seven'], 'eight']]]],
    string = array.map(function join(a) { 
        return Array.isArray(a) ? a.map(join).join(';') : a;
    }).join(";");

console.log(string);

It's a nested array, with the array being in the zeroth index, but you are joining the parent array. Use:

arr[0].join(';');

This takes the first index of the array and joins it.

var arr = [
  ["one", "two", "three"]
];
console.log(arr[0].join(';'));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742414917a4439552.html

相关推荐

  • javascript - Using join method in nested array - Stack Overflow

    i have this nested array arr: [[ "one", "two" , "three"]]I want to extra

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信