如何从json字符串中获取jquery中的特定对象值

时间:2023-02-09 08:48:41

This is how I get data inside jQuery:

这就是我在jQuery中获取数据的方法:

    var jsonList = '[{"region":"NCA","depprt":"Havana, Cuba"},{"region":"NCA","depprt":"Havana, Cuba"}]';

  var jsList = JSON.parse(jsonList);
  var nesels = $.trim(sel);
  var ncaList = jsList.filter(function (obj) { return obj.region == nesels; });

In here ncaList provide filtered data. Now I want to get only depprt from the filtered data to the array without any duplicates. How can I do that?

在这里,ncaList提供过滤后的数据。现在我想从过滤后的数据中只得到一个没有任何重复的数据。我怎样才能做到这一点?

2 个解决方案

#1


1  

You can use .map() to extract only depprt like

您可以使用.map()来仅提取depprt

ncaList = ncaList.map(function(o){
     return o.depprt; 
}).filter(onlyUnique);

With reference to @TLindig answer

参考@TLindig的答案

function onlyUnique(value, index, self) { 
    return self.indexOf(value) === index;
}

#2


0  

Supposing that "ncalist" is also in json format as jslist.

假设“ncalist”也是json格式的jslist。

You can traverse and get required info/fields as :

您可以遍历并获取所需的信息/字段:

for(i = 0; i < ncalist.length; i++){
	var depprtVar = ncalist[i]["depprt"];
    // here you can write code to check for duplication in your array and add the depprtVar to array if it is not in the array.

}

#1


1  

You can use .map() to extract only depprt like

您可以使用.map()来仅提取depprt

ncaList = ncaList.map(function(o){
     return o.depprt; 
}).filter(onlyUnique);

With reference to @TLindig answer

参考@TLindig的答案

function onlyUnique(value, index, self) { 
    return self.indexOf(value) === index;
}

#2


0  

Supposing that "ncalist" is also in json format as jslist.

假设“ncalist”也是json格式的jslist。

You can traverse and get required info/fields as :

您可以遍历并获取所需的信息/字段:

for(i = 0; i < ncalist.length; i++){
	var depprtVar = ncalist[i]["depprt"];
    // here you can write code to check for duplication in your array and add the depprtVar to array if it is not in the array.

}