javascript - How to get collection from JSON array -


i have array of json in ajax response in javascript.

[{"id":1,"text":"apple"},{"id":2,"text":"mango"},{"id":3,"text":"banana"}] 

i want extract comma separated list of ids "1, 2, 3" json response. how can this?

first, parse json (if didn't already):

var arr = json.parse('[{"id":1,"text":"apple"},{"id":2,"text":"mango"},{"id":3,"text":"banana"}]'); 

then loop array extract id on each object. can use map method of arrays loop , add ids new array in 1 go:

var ids = arr.map(function(item) {     return item.id; }); alert(ids.join(',')); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -