0

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.

1
  • This didn't work..? var result = objArray.map(function(a) {return a.libelle;}); Commented Apr 21, 2016 at 21:13

2 Answers 2

2

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; });
Sign up to request clarification or add additional context in comments.

2 Comments

Exactly what i wanted!! Thank you sir. :)
No problem! If that worked for you could you accept? Thanks
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.