1

Hi I am having an array of objects like this

  [{"$value":"Cardiology","$id":"0","$priority":null},                                                 
  {"$value":"Dermatology","$id":"1","$priority":null},
  {"$value":"Physiology","$id":"2","$priority":null},{"$value":"Child   
  Physiology","$id":"3","$priority":null}] 

I would like to convert this into

   ['Cardiology','Dermatology','Physiology','Child Physiology']

any help will be appreciated.

Karthik VK
  • 11
  • 4

2 Answers2

1

You need to iterate over the array and create another array.

var arr = [{"$value":"Cardiology","$id":"0","$priority":null},                                                 
  {"$value":"Dermatology","$id":"1","$priority":null},
  {"$value":"Physiology","$id":"2","$priority":null},{"$value":"Child Physiology","$id":"3","$priority":null}];
var arr1 = [];

for (var i = 0 ; i < arr.length; i++) {
   arr1.push(arr[i].$value);
}
console.log(arr1);
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59
1

You can use Array.map()

var array = [{
  "$value": "Cardiology",
  "$id": "0",
  "$priority": null
}, {
  "$value": "Dermatology",
  "$id": "1",
  "$priority": null
}, {
  "$value": "Physiology",
  "$id": "2",
  "$priority": null
}, {
  "$value": "Child Physiology",
  "$id": "3",
  "$priority": null
}]

var newarray = array.map(function(obj) {
  return obj.$value
});

snippet.log(JSON.stringify(newarray))
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<!-- To show result in the dom instead of console, only to be used in the snippet not in production -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531