【问题标题】:how to get specific object value inside jquery from json string如何从json字符串中获取jquery中的特定对象值
【发布时间】:2016-04-04 07:01:28
【问题描述】:

这就是我在 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 到数组中,而没有任何重复。我该怎么做?

【问题讨论】:

  • 你能告诉我们你的sel字符串吗?
  • 您可以在ncaList = ncaList.map(function(o){return o.depprt; })上使用map get,然后您可以使用各种方法获得独特的价值
  • @Satpal ys 它正在工作把这个作为答案。我会投票。同时我怎样才能删除重复的值
  • 在这个线程中使用了.each()方法stackoverflow.com/questions/15219435/…

标签: jquery json duplicates


【解决方案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;
}

【讨论】:

    【解决方案2】:

    假设“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.
    
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多