3

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.

asked Oct 4, 2016 at 10:27
1
  • 2
    Use arr[0].join(';');. The array is nested array. Commented Oct 4, 2016 at 10:27

3 Answers 3

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

answered Oct 4, 2016 at 10:29
Sign up to request clarification or add additional context in comments.

Comments

2

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

answered Oct 4, 2016 at 10:59

Comments

1

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

answered Oct 4, 2016 at 10:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.