Reputation: 507
I'm currently using jquery and javascript to get json data, which I would like to add to a list. The raw data received from it's source looks like this;
null({"groups": "Blacksheep,Family,Voice,Technical,Band"})
with a bit of code my input comes out as
Blacksheep,Family,Voice,Technical,Band
My code takes these values and puts them though a for loop, looping once for each entry. it then takes that entry's name and puts it into a <li>
field on my html document inside of a <ul>
tag named people
.
for (i=0 ; i < input.length ; i++){
console.log(input);
document.getElementById("people").innerHTML += "<li>" + input[i] + "</li>";
}
I tried to turn it into an array but adding a '[' on each side, but obviously it isn't so easy. at the moment it takes the json entry and lists each letter instead of word.
is there a way to separate each word from json? or a way to interpret it as an array?
Upvotes: 3
Views: 175
Reputation: 497
This should do it. As all the answer mention split the string.
var arr = "Blacksheep,Family,Voice,Technical,Band";
var input = arr.split(",");
for (i=0 ; i < input.length ; i++){
console.log(input);
document.getElementById("people").innerHTML += "<li>" + input[i] + "</li>";
}
Upvotes: 2
Reputation: 119877
The data is a string. you can use .split(delimiter)
to split the words.
//split by ",". now input is an array
input = input.split(',');
Upvotes: 3
Reputation: 358
you can split the var
"Blacksheep,Family,Voice,Technical,Band".split(',');
this returning a string of array..
Upvotes: 7