繁体   English   中英

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

[英]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);

参考@TLindig 回答

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM