如何使用特定值将json数组转换为javascript数组

时间:2021-12-28 21:37:58

i want to convert a json array (string) to javascript array using just some specific values. The json array is :

我想使用一些特定的值将json数组(字符串)转换为javascript数组。 json数组是:

[{"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.

我希望得到这样的东西[“famille de test”,“GEOLOCALISATION”,......]只使用libelle值。我试图使用$ .map但没有成功。

2 个解决方案

#1


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; });

#2


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

首先,您必须使用JSON.parse(yourJSONString)将JSON字符串转换为JavaScript数组。之后,它是一个简单的JavaScript数组,您可以使用您尝试过的map方法

#1


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; });

#2


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

首先,您必须使用JSON.parse(yourJSONString)将JSON字符串转换为JavaScript数组。之后,它是一个简单的JavaScript数组,您可以使用您尝试过的map方法