Question

Here I have this code jquery:

JSON.stringify($("#vrsta_rada").select2("data"));

and I get this JSON file:

"[{"id":2,"text":"setva/zasad"},{"id":6,"text":"orezivanje"},{"id":8,"text":"skladistenje"}]"

How I can use .each function to get this string: data[0].text, data[1].text ... so to get string from JSON above: setva/zasad, orezivanje, skladistenje

?

Was it helpful?

Solution

I'm not sure why you are stringifying the data if you want to manipulate it. So this will work:

var data = $("#vrsta_rada").select2("data");

$.each(data, function(key, value) {
  var text = data[key].text
  //or
  var text = value.text
});

Is this what you are looking for? That will iterate through the list. I prefer to use data[key] because you can then manipulate the data and it will change the original whereas value only makes a copy.

OTHER TIPS

Try

$.each(data, function(index, value) {
   alert(value.text)
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top