Reputation: 619
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.
Upvotes: 3
Views: 4155
Reputation: 386519
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);
Upvotes: 2
Reputation: 68373
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 );
Upvotes: 3
Reputation: 167162
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(';'));
Upvotes: 1