0

How can I extract values from json array:

[{"id":1,"name":"Blue"}, {"id":2,"name":"Green"}, {"id":3,"name":"Red"}]

and return id's only in following format:

['1','2','3','4','5']
Alko
  • 1,421
  • 5
  • 25
  • 51

1 Answers1

0

try this,you can use map() function on arrays

var x = [{
  "id": 1,
  "name": "Blue"
}, {
  "id": 2,
  "name": "Green"
}, {
  "id": 3,
  "name": "Red"
}];
var result = x.map(function(v) {
  return v.id + ''
})
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>')
Pavan Teja
  • 3,192
  • 1
  • 15
  • 22