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
];
Jan Černý
1,4162 gold badges22 silver badges39 bronze badges
asked Apr 23, 2018 at 10:10

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

answered Apr 23, 2018 at 10:15
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);

answered Apr 23, 2018 at 10:17

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:

answered Apr 23, 2018 at 10:12

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

answered Apr 23, 2018 at 10:14

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.