-4

JSON Array:

[ { sw: 'NODE.JS' }, { sw: 'Heroku Toolbelt' } ]

need to convert it to:

['NODE.JS','Heroku Toolbelt']. 

Any quickest help will be highly apprecaited.

potashin
  • 44,205
  • 11
  • 83
  • 107
user3421622
  • 89
  • 1
  • 10
  • JSON is a string. "JSON Array" is not a thing. What you have is an array of objects, that you want to convert to an array based on a property of those objects. – Niet the Dark Absol Apr 15 '14 at 15:35
  • What's the problem? You don't know how to use a `for` loop? or you don't know how to add items to a new Array? – cookie monster Apr 15 '14 at 15:40
  • possible duplicate of [javascript array of objects, extract value of a property as array](http://stackoverflow.com/questions/19590865/javascript-array-of-objects-extract-value-of-a-property-as-array) – Felix Kling Apr 15 '14 at 15:44

1 Answers1

1

I'd use Array.prototype.map:

var new_array = arr.map(function(e) {
    return e.sw;
});

Don't forget to get the actual JavaScript array from the JSON string with JSON.parse (as an example).

VisioN
  • 143,310
  • 32
  • 282
  • 281