[英]how to get specific object value inside jquery from json string
这就是我在 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; });
在这里ncaList
提供过滤后的数据。 现在我只想从过滤后的数据中获取depprt
到数组,而没有任何重复项。 我怎样才能做到这一点?
您可以使用.map()
仅提取depprt
类的
ncaList = ncaList.map(function(o){
return o.depprt;
}).filter(onlyUnique);
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
假设“ ncalist ”也是 json 格式的jslist 。
您可以遍历并获取所需的信息/字段:
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. }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.