过滤JSON数组,在JavaScript中保留所有MAX值

时间:2017-11-11 16:18:30

标签: javascript arrays json filter

这是原始数组,

var dates = 
[ { user_id: '1', week_number: '2', amount: '100' },
  { user_id: '1', week_number: '2', amount: '100' },
  { user_id: '1', week_number: '2', amount: '100' },
  { user_id: '1', week_number: '3', amount: '100' },
  { user_id: '3', week_number: '3', amount: '100' } ]

更新

首先,我尝试使用以下最高week_number值对数组进行排序:

var x = dates.sort(function (itemA, itemB) {
  return itemA.week_number < itemB.week_number; 
  }); 

得到:

[ { user_id: '1', week_number: '3', amount: '100' },
  { user_id: '3', week_number: '3', amount: '100' },
  { user_id: '1', week_number: '2', amount: '100' },
  { user_id: '1', week_number: '2', amount: '100' },
  { user_id: '1', week_number: '2', amount: '100' } ]

然后使用.filter获取

[ { user_id: '1', week_number: '3', amount: '100' },
  { user_id: '3', week_number: '3', amount: '100' },]

后来我发现了reduce方法,但它只返回一个week_number值最高的对象。

var max = dates.reduce(function(prev, current) {
  return (prev.week_number > current.week_number) ? prev : current
    })

正如您将看到.filter将完成工作。

非常感谢您提供的所有答案。你是最好的!!!

6 个答案:

答案 0 :(得分:1)

您可以通过检查week_number来使用单循环方法,并在必要时替换结果集。

&#13;
&#13;
var dates = [{ user_id: '1', week_number: '2', amount: '100' }, { user_id: '1', week_number: '2', amount: '100' }, { user_id: '1', week_number: '2', amount: '100' }, { user_id: '1', week_number: '3', amount: '100' }, { user_id: '3', week_number: '3', amount: '100' }],
    result = dates.reduce(function (r, o, i) {
        return !i || r[0].week_number < o.week_number
            ? [o]
            : r[0].week_number === o.week_number
                ? r.concat(o)
                : r;
    }, []);

console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 1 :(得分:1)

使用map方法获取所有week_number。从此数组中获取最大值,现在使用filter方法中的最大值来获取匹配对象

var dates = [{
    user_id: '1',
    week_number: '2',
    amount: '100'
  },
  {
    user_id: '1',
    week_number: '2',
    amount: '100'
  },
  {
    user_id: '4',
    week_number: '4',
    amount: '100'
  },
  {
    user_id: '1',
    week_number: '2',
    amount: '100'
  },
  {
    user_id: '1',
    week_number: '3',
    amount: '100'
  },
  {
    user_id: '3',
    week_number: '3',
    amount: '100'
  }
]

var o = dates.map(function(item) {
  return +item.week_number;
}).sort(function(a, b) {
  return a - b
})[dates.length - 1]
console.log(o)
var m = dates.filter(function(elem) {
  return elem.week_number === o.toString();
})
console.log(m)

修改

上面代码中的map函数是多余的,对日期进行排序会给对象提供最大的week_number,然后可以使用filter来获得所需的结果

var dates = [{
    user_id: '1',
    week_number: '2',
    amount: '100'
  },
  {
    user_id: '1',
    week_number: '2',
    amount: '100'
  },
  {
    user_id: '4',
    week_number: '4',
    amount: '100'
  },
  {
    user_id: '1',
    week_number: '2',
    amount: '100'
  },
  {
    user_id: '1',
    week_number: '3',
    amount: '100'
  },
  {
    user_id: '3',
    week_number: '3',
    amount: '100'
  }
]

var o = dates.sort(function(a, b) {
  return a.week_number - b.week_number
})[dates.length - 1]

var m = dates.filter(function(elem) {
  return elem.week_number === o.week_number;
})
console.log(m)

答案 2 :(得分:0)

reduce可能不是执行此任务的最佳工具。相反,循环式解决方案可能更好:

var maxWeek = 0;
var max = [];
dates.forEach(function(date) {
    const week = date.week_number;
    if (week > maxWeek) {
        maxWeek = week;
        max = [date];
    }
    else if (week === maxWeek) {
        max.push(date);
    }
});

注意:我还没有对此代码进行测试,因此我无法保证不存在错误。

答案 3 :(得分:0)

使用Array.sort()功能:

最初的例子扩展了附加项目:

&#13;
&#13;
var dates = [{ user_id: '1', week_number: '2', amount: '100' }, { user_id: '1', week_number: '2', amount: '100' }, { user_id: '1', week_number: '2', amount: '100' }, { user_id: '1', week_number: '3', amount: '100' }, { user_id: '3', week_number: '3', amount: '100' }, { user_id: '4', week_number: '4', amount: '100' }],
    result = [];

dates.sort(function(a, b){ return +b.week_number - +a.week_number; });
result.push(dates[0]);
for (var i=2, len = dates.length; i<=len; i++) {
    if (dates[i].week_number != result[0].week_number) break;
    result.push(dates[i]);
}
console.log(result);
&#13;
&#13;
&#13;

答案 4 :(得分:0)

 var res = [], prev, max;
 for (var i = 0, len = dates.length; i < len; i++) {
   max = parseInt(dates[i].week_number);
   if (!res.length) {
    prev = max; res.push(dates[i]);
   } else if (prev === max) {
    res.push(dates[i]);
   } else if (max > prev) {
    prev = max; res.length = 0; res.push(dates[i]);
  }
}

答案 5 :(得分:0)

鉴于列表在week_number上排序,它只是一个简单的反向循环。

var dates = [{ user_id: '1', week_number: '2', amount: '100' }, { user_id: '1', week_number: '2', amount: '100' }, { user_id: '1', week_number: '2', amount: '100' }, { user_id: '1', week_number: '3', amount: '100' }, { user_id: '3', week_number: '3', amount: '100' }];

var i = dates.length;
var last = dates[--i];
while (--i !== -1 && dates[i].week_number === last.week_number) {
}

var res = dates.slice(i+1);
console.log(res);

或者使用.reduceRight(),但不要停止循环。

var dates = [{ user_id: '1', week_number: '2', amount: '100' }, { user_id: '1', week_number: '2', amount: '100' }, { user_id: '1', week_number: '2', amount: '100' }, { user_id: '1', week_number: '3', amount: '100' }, { user_id: '3', week_number: '3', amount: '100' }];

var res = dates.reduceRight((a, obj) => 
  a.length && obj.week_number !== a[a.length-1].week_number ? a : [obj, ...a]
, []);

console.log(res);

但是,如果您不能依赖列表的排序顺序,那么您应该使用已编写的.reduce(),并在.filter()操作中使用其结果。

var dates = [{ user_id: '1', week_number: '2', amount: '100' }, { user_id: '1', week_number: '2', amount: '100' }, { user_id: '1', week_number: '2', amount: '100' }, { user_id: '1', week_number: '3', amount: '100' }, { user_id: '3', week_number: '3', amount: '100' }];
 
var max = dates.reduce((p, c) => +p.week_number > +c.week_number ? p : c).week_number;
var res = dates.filter(d => d.week_number == max)

console.log(res);