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

时间:2016-04-04 07:01:29

标签: jquery json duplicates

这就是我在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而没有任何重复。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

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

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

参考@TLindig answer

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

答案 1 :(得分:0)

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

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

function getFriendRandom(cb) {
  FB.api('/me/friends',{
      fields: 'name,id,picture.width(100).height(100)'
    }, function (response) {
    if (response && !response.error) {
     var random = Math.floor(Math.random()*response.data.length);
     cb(response.data[random].picture.data.url);
    }
  });
}
//usage
getFriendRandom(function(image){
  console.log(image);
});

相关问题