【问题标题】:filter json object array for unique values过滤 json 对象数组以获得唯一值
【发布时间】:2014-07-25 17:06:20
【问题描述】:

我有一个 json 对象,它是一个数组数组。它看起来像这样:

[{county: Adams, candidate: Smith, votes: 5},
{county: Adams, candidate: Jones, votes: 1},
{county: Clay, candidate: Jones, votes: 7},
{county: Clay, candidate: Smith, votes: 5},
{county: York, candidate: Jones, votes: 10},
{county: York, candidate: Smith, votes: 9}]

对数组进行排序,使得每个县中得票最多的候选人是该县的第一个出现的项目。

我想过滤数组,只显示每个县的得票最多的候选人。

最好的方法是什么?我尝试使用 pop() 删除具有相同县名的项目,但 pop 只是删除了数组中的最后一项。我在看 splice,但我不确定是否有更直接的方法来做到这一点。

jquery:

$.ajax({
    url: "myquerypage.php",
    method: "POST",
    success: function(data) {
        var obj = jQuery.parseJSON(data); //this is my json array of rows from the dB
        var obj2 = obj;                   //I made a copy of the original json array
        var testString = obj2[0].county;  
        for (var i = 0; i < obj2.length; i++) {
            if (obj2[i].county == testString) {
                obj2[i].pop();            //this is wrong
            } else {
                testString = obj2[i].county;
            }
        }

        console.log(obj2);
});

【问题讨论】:

  • 您知道您的 JSON 中缺少冒号 (":") 吗?这是第三项,在“投票”和 7 之间。顺便说一下,如果 JSON 已经按(首先)“县”和(其次)“投票”排序,那么您只需抓住第一项每个县,对吧?
  • @BinaryCat:好的。已更正。但没关系,因为实际的数组没有缺少冒号。
  • 如果您要进行大量数据操作,您是否考虑过添加类似 UnderscoreJS 的内容以简化操作?
  • 看看我发布的 jsfiddle。它返回一个由三个对象组成的数组 - 每个县获得最多票的对象。

标签: jquery arrays json


【解决方案1】:

这是underscore 版本(fiddle):

// The output
var results = []; 
// Get a list of the unique countys
var countys = _.uniq(_.pluck(arr, 'county'));

// Iterate over the unique countys
_.each(countys, function(county) {
    // Find all the results for this county and save the one with the most votes
    results.push(_.max(_.where(arr, { county: county }), function(result) {
        return result.votes;
    }));
});

console.log(results);

您可能会比使用下划线更巧妙地执行此操作 - 这对于此类事情非常有用,并且如 cmets 中所述,如果您要进行大量数据操作,则值得进一步研究。

【讨论】:

  • 说结果是 [-Infinity]
  • jsfiddle 对我有用。那是您看到无穷大结果的地方吗?
【解决方案2】:

你可以像这样使用reduce函数

$.ajax({
url: "myquerypage.php",
method: "POST",
success: function(data) {
    var obj = jQuery.parseJSON(data); //this is my json array of rows from the dB
    var distinctObj = obj.reduce(
        function(acc, el){
            var county = acc.set[el.county];
            if (county === undefined){
                acc.set[el.county] = county = {};
            }
            if(county.candidate === undefined){
                acc.result.push(el);
                county.candidate = el;
                county.index = acc.result.length-1;
            }else if(county.candidate.votes < el.votes){
                county.candidate = el;
                acc.result[county.index] = county.candidate;
            }
            return acc;
        },{set:{},result:[]}).result;
    //console.log(distinctObj);
    console.log(JSON.stringify(distinctObj));
});

更新

为了输出所有信息,你可以使用类似这样的东西

distinctObj.forEach(function(el){
    console.log(JSON.stringify(el));
});

console.log(JSON.stringify(distinctObj));

【讨论】:

  • @LauraNMS 是的,在控制台中你可以看到这个数组中的所有对象,你需要什么输出?
  • 示例控制台中的 @LauraNMS 还输出对象数组
  • 我需要看看对象是什么(它们的值)
猜你喜欢
  • 2010-10-10
  • 1970-01-01
  • 2013-06-22
  • 1970-01-01
  • 2017-09-08
  • 2021-09-08
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多