1

I have one array like this:

temp = [
'data1',
['data1_a','data1_b'],
['data2_a','data2_b','data2_c']
];
//

I want to change the array within my array using toString(), how the best to do it? Then the array like :

temp = [
'data1',
'data1_a,data1_b',
'data2_a,data2_b,data2_c
];

4 Answers 4

6

use map and get that desired output. No need for any other complex methods or functions,

var temp = [
  'data1',
  ['data1_a','data1_b'],
  ['data2_a','data2_b','data2_c']
];


var res = temp.map((item) => item.toString());

console.log(res);

Sign up to request clarification or add additional context in comments.

Comments

3

Just map it and return the stringified version. There's no need for any checks - the strings will just be unmodified, the arrays will be converted like you want them

temp = [
  'data1', ['data1_a', 'data1_b'],
  ['data2_a', 'data2_b', 'data2_c']
];

let foo = temp.map(String);
console.log(foo);

1 Comment

but if i provide NULL in array, this NULL will be 'null', how to give this empty??
1

You can use .map() with .join():

let temp = [
  'data1',
  ['data1_a','data1_b'],
  ['data2_a','data2_b','data2_c']
];

let result = temp.map(v => Array.isArray(v) ? v.join(",") : v);

console.log(result);

Docs:

Comments

0

Use this

temp = [
'data1',
['data1_a','data1_b'],
['data2_a','data2_b','data2_c']
];

for(var i = 0; i < temp.length; i ++) {
    temp[i] += "";
}

console.log(temp);

Comments

Your Answer

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.