-2

I am new to stack overflow learning javascript and programming.
I have a problem that i am stuck while learning and thinking any help on this question will be useful for me thanks and the question is:
Example i have a variable a in the code below and i want to convert it to an array in javascript

var a = ["baby,cat,dog"]
i wanted it to be
a = ["baby","cat","dog"].

0

2 Answers 2

3

Use String.prototype.split() as below

var a = ["baby,cat,dog"];
a = a[0].split(',');
console.log(a);

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

1 Comment

thank you so much for you valuable time it helped me i was thinking like i already have the , in the array and i if use split it won't work thanks but it did work thanks again
2

You could take the array and map the splitted values and get a flat array back.

var array = ["baby,cat,dog"];
    result = array.flatMap(s => s.split(','));

console.log(result);

1 Comment

thank you so much for you response i took the answer from @harunur Rashid

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.