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
Post a Comment