如何在具有重复属性名称的对象中获取最大属性值? [英] How to get max property value in an object with duplicate property name?
问题描述
假设我有一个具有以下值的数组:
Suppose I have an array with the following values:
数组名称为订户"
1: {month: "2019-07-24", subs: 2}
2: {month: "2019-07-31", subs: 3}
3: {month: "2019-08-01", subs: 2}
4: {month: "2019-08-02", subs: 3}
5: {month: "2019-08-05", subs: 3}
6: {month: "2019-08-08", subs: 4}
7: {month: "2019-08-14", subs: 5}
8: {month: "2019-08-20", subs: 7}
9: {month: "2019-08-23", subs: 7}
10: {month: "2019-08-28", subs: 8}
11: {month: "2019-08-29", subs: 11}
12: {month: "2019-09-02", subs: 2}
13: {month: "2019-09-03", subs: 2}
14: {month: "2019-09-04", subs: 3}
15: {month: "2019-09-05", subs: 5}
16: {month: "2019-09-06", subs: 5}
17: {month: "2019-09-09", subs: 6}
18: {month: "2019-09-10", subs: 7}
19: {month: "2019-09-11", subs: 8}
20: {month: "2019-09-12", subs: 9}
我的问题是,我如何才能按月获得最高的费用?
My question is, how can I get the highest subs by monthly?
预期输出:
0: {month: "July", subs: 3}
1: {month: "August", subs: 11}
2: {month: "September", subs: 9}
我尝试使用array.map,但无法获得预期的输出.我也尝试使用Math.max,但似乎无法在对象中使用它.
I tried using array.map, but I cannot get the expected output. Also I tried using Math.max, but I cannot seem to use it in an object.
var getMonthlyValues = subscribers.map(value => {
var obj = {};
var dateName = new Date(value.period).toLocaleString('default', { month: 'long' });
obj = {month:dateName, subs:value.monthly}
return obj;
})
例如,如果这些来自数据库,其中列分别为month和sub,并具有各自的值,则可以执行此操作.但是我该如何在纯JS上做到这一点?
I can do this if for example these are from database with columns as month and sub, with their respective values. But how can I do this on pure JS?
推荐答案
您可以使用 Map
,并为每个month
收集最大的subs
.
You could take a Map
, and collect the maximum subs
for every month
.
分步进行:
-
通过使用
Map
作为累加器并使用分组属性month
作为键来减少数组.作为值,请从先前存储的值或零与实际值中取最大值.
Reduce the array by taking a
Map
as accumulator and take the grouping propertymonth
as key. As value take the maximum from either a previously stored value or zero and the actual value.
如果可以使用负值或零,请使用条件(三元)运算符?:
,例如
If negative values or zeros are available use a conditional (ternary) operator ?:
, like
m.has(month) ? Math.max(m.get(month), subs) : subs
Take the list of key/value pairs and return an array of objects with custom named properties with Array.from
and a mapping function.
var data = [{ month: "July", subs: 2 }, { month: "July", subs: 3 }, { month: "August", subs: 2 }, { month: "August", subs: 3 }, { month: "August", subs: 3 }, { month: "August", subs: 4 }, { month: "August", subs: 5 }, { month: "August", subs: 7 }, { month: "August", subs: 7 }, { month: "August", subs: 8 }, { month: "August", subs: 11 }, { month: "September", subs: 2 }, { month: "September", subs: 2 }, { month: "September", subs: 3 }, { month: "September", subs: 5 }, { month: "September", subs: 5 }, { month: "September", subs: 6 }, { month: "September", subs: 7 }, { month: "September", subs: 8 }, { month: "September", subs: 9 }],
result = Array.from(
data.reduce(
(m, { month, subs }) => m.set(month, Math.max(m.get(month) || 0, subs)),
new Map
),
([month, subs]) => ({ month, subs })
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
这篇关于如何在具有重复属性名称的对象中获取最大属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!