Skip to content
Advertisement

How to convert a value fetched from json file to a list in javascript

I have a string with comma separated values in json file and want to convert it to a list to iterate through for loop and click on each element.Please help me on this !

testdata.json :

JavaScript

code:

JavaScript

output :

JavaScript

Advertisement

Answer

I think the easiest way is to do it by parts, you can first remove the - using the split function, then it will result into an array like:

JavaScript

then iterating over each one of the strings and using split you will be able to separate the strings that has ,. resulting in something like:

JavaScript

then you can flat this array using the function flat of the arrays.

here you ahve a working sample:

JavaScript
Advertisement