Reputation: 1971
i want to convert a json array (string) to javascript array using just some specific values. The json array is :
[{"id":47,"libelle":"famille de test"},{"id":1,"libelle":"GEOLOCALISATION"},{"id":4,"libelle":"OUTILS"},{"id":2,"libelle":"PROPRETE"},{"id":3,"libelle":"URGENCE"}]
and i want to get something like this ["famille de test", "GEOLOCALISATION", ...] using just libelle values. I tried to use $.map but didn't work out.
Upvotes: 0
Views: 54
Reputation: 146
First, you must turn your JSON string into a JavaScript Array by using JSON.parse(yourJSONString)
. After that, it is a simple JavaScript array and you can use the map method you tried
Upvotes: 0
Reputation: 4050
The map implementation should work:
var jsonStr = '[{"id":47,"libelle":"famille de test"},{"id":1,"libelle":"GEOLOCALISATION"},{"id":4,"libelle":"OUTILS"},{"id":2,"libelle":"PROPRETE"},{"id":3,"libelle":"URGENCE"}]';
var arr = JSON.parse(jsonStr);
var libelle = arr.map(function(x) { return x.libelle; });
Upvotes: 2