我如何根据日期和 ID 从数组中总结一个值
how do i sum up a value from an array depending on the date and id
我有以下数组实例
this.array=[{id:"121",score:"5",createdOn:"2022-05-17T19:52:23.6846702+00:00"}
{id:"121",score:"8",createdOn:"2022-05-19T13:00:00.6846702+00:00"}
{id:"122",score:"7",createdOn:"2022-04-11T08:00:00.6846702+00:00"}
{id:"121",score:"1",createdOn:"2022-03-12T12:00:00.6846702+00:00"}
]
如何使用匹配的 ID 和月份来获取一些值。
例如我的输出应该是
newArray=[{id:"121",score:13,month:"May"}
{id:"122",score:7,month:"April"}
{id:"121",score:1,month:"March"}
]
我试过这样的东西
this.array.forEach(item => {
var obj = {}
if (obj.hasOwnProperty(item.id)) {
obj["id"] = obj[item.id] + parseFloat(item.score);
}
else {
obj["id"] = parseFloat(item.score);
}
this.newArray.push(obj);
});
但它没有用,我不知道如何查看月份
您可以使用 id_month
.
的复合键来使用相当标准的 'group by'
该示例使用 reduce()
to iterate the array, toLocaleDateString()
to retrieve the month name from the ISO date string and a template literal 创建复合键。
score
应在添加前转换为数字以避免 accidental concatenation, here using the unary plus (+) 运算符。
最后我们只取分组对象的Object.values
作为结果。
const array = [{ id: "121", score: "5", createdOn: "2022-05-17T19:52:23.6846702+00:00" }, { id: "121", score: "8", createdOn: "2022-05-19T13:00:00.6846702+00:00" }, { id: "122", score: "7", createdOn: "2022-04-11T08:00:00.6846702+00:00" }, { id: "121", score: "1", createdOn: "2022-03-12T12:00:00.6846702+00:00" },];
const result = Object.values(
array.reduce((a, { id, createdOn, score, ...rest }) => {
const month = new Date(createdOn).toLocaleDateString('en', { month: 'long' });
a[`${id}_${month}`] ??= { id, month, score: 0, ...rest };
a[`${id}_${month}`].score += +score;
return a;
}, {})
)
console.log(result)
如果您愿意,可以在标准 for...of
循环中使用完全相同的逻辑,而不是在 reduce()
调用中使用。
const array = [{ id: "121", score: "5", createdOn: "2022-05-17T19:52:23.6846702+00:00" }, { id: "121", score: "8", createdOn: "2022-05-19T13:00:00.6846702+00:00" }, { id: "122", score: "7", createdOn: "2022-04-11T08:00:00.6846702+00:00" }, { id: "121", score: "1", createdOn: "2022-03-12T12:00:00.6846702+00:00" },];
const grouped = {}
for (const { id, createdOn, score, ...rest } of array) {
const month = new Date(createdOn).toLocaleDateString('en', { month: 'long' });
grouped[`${id}_${month}`] ??= { id, month, score: 0, ...rest }
grouped[`${id}_${month}`].score += +score;
}
const result = Object.values(grouped);
console.log(result)
使用临时对象保存更新的信息。
因为您需要分离出具有相同 ID 但创建于不同月份的对象,您可以在临时对象上使用键 id-month
来识别它们。
遍历对象数组 getting the month name,并创建密钥。 (我在这里使用 Intl.DateTimeFormat
是因为您可以传入语言字符串以获得与函数不同的结果 - 例如尝试 'es-ES'
。)
如果属性在临时对象上用那个键doesn't exist添加一个默认对象给临时对象一个默认对象值,然后增加它的score
(确保先将其强制转换为数字)。
最终得到 Object.values
,它将 return 临时对象中所有这些值的数组。
const arr=[{id:"121",score:"5",createdOn:"2022-05-17T19:52:23.6846702+00:00"},{id:"121",score:"8",createdOn:"2022-05-19T13:00:00.6846702+00:00"},{id:"122",score:"7",createdOn:"2022-04-11T08:00:00.6846702+00:00"},{id:"121",score:"1",createdOn:"2022-03-12T12:00:00.6846702+00:00"}];
const temp = {};
const language = 'en-GB';
function getMonth(createdOn, language) {
const date = new Date(createdOn);
return new Intl.DateTimeFormat(language, { month: 'long' }).format(date);
}
for (const obj of arr) {
const { id, score, createdOn } = obj;
const month = getMonth(createdOn, language);
const key = `${id}-${month}`;
temp[key] ??= { id, score: 0, month };
temp[key].score += Number(score);
}
console.log(Object.values(temp));
其他文档
我有以下数组实例
this.array=[{id:"121",score:"5",createdOn:"2022-05-17T19:52:23.6846702+00:00"}
{id:"121",score:"8",createdOn:"2022-05-19T13:00:00.6846702+00:00"}
{id:"122",score:"7",createdOn:"2022-04-11T08:00:00.6846702+00:00"}
{id:"121",score:"1",createdOn:"2022-03-12T12:00:00.6846702+00:00"}
]
如何使用匹配的 ID 和月份来获取一些值。 例如我的输出应该是
newArray=[{id:"121",score:13,month:"May"}
{id:"122",score:7,month:"April"}
{id:"121",score:1,month:"March"}
]
我试过这样的东西
this.array.forEach(item => {
var obj = {}
if (obj.hasOwnProperty(item.id)) {
obj["id"] = obj[item.id] + parseFloat(item.score);
}
else {
obj["id"] = parseFloat(item.score);
}
this.newArray.push(obj);
});
但它没有用,我不知道如何查看月份
您可以使用 id_month
.
该示例使用 reduce()
to iterate the array, toLocaleDateString()
to retrieve the month name from the ISO date string and a template literal 创建复合键。
score
应在添加前转换为数字以避免 accidental concatenation, here using the unary plus (+) 运算符。
最后我们只取分组对象的Object.values
作为结果。
const array = [{ id: "121", score: "5", createdOn: "2022-05-17T19:52:23.6846702+00:00" }, { id: "121", score: "8", createdOn: "2022-05-19T13:00:00.6846702+00:00" }, { id: "122", score: "7", createdOn: "2022-04-11T08:00:00.6846702+00:00" }, { id: "121", score: "1", createdOn: "2022-03-12T12:00:00.6846702+00:00" },];
const result = Object.values(
array.reduce((a, { id, createdOn, score, ...rest }) => {
const month = new Date(createdOn).toLocaleDateString('en', { month: 'long' });
a[`${id}_${month}`] ??= { id, month, score: 0, ...rest };
a[`${id}_${month}`].score += +score;
return a;
}, {})
)
console.log(result)
如果您愿意,可以在标准 for...of
循环中使用完全相同的逻辑,而不是在 reduce()
调用中使用。
const array = [{ id: "121", score: "5", createdOn: "2022-05-17T19:52:23.6846702+00:00" }, { id: "121", score: "8", createdOn: "2022-05-19T13:00:00.6846702+00:00" }, { id: "122", score: "7", createdOn: "2022-04-11T08:00:00.6846702+00:00" }, { id: "121", score: "1", createdOn: "2022-03-12T12:00:00.6846702+00:00" },];
const grouped = {}
for (const { id, createdOn, score, ...rest } of array) {
const month = new Date(createdOn).toLocaleDateString('en', { month: 'long' });
grouped[`${id}_${month}`] ??= { id, month, score: 0, ...rest }
grouped[`${id}_${month}`].score += +score;
}
const result = Object.values(grouped);
console.log(result)
使用临时对象保存更新的信息。
因为您需要分离出具有相同 ID 但创建于不同月份的对象,您可以在临时对象上使用键
id-month
来识别它们。遍历对象数组 getting the month name,并创建密钥。 (我在这里使用
Intl.DateTimeFormat
是因为您可以传入语言字符串以获得与函数不同的结果 - 例如尝试'es-ES'
。)如果属性在临时对象上用那个键doesn't exist添加一个默认对象给临时对象一个默认对象值,然后增加它的
score
(确保先将其强制转换为数字)。最终得到
Object.values
,它将 return 临时对象中所有这些值的数组。
const arr=[{id:"121",score:"5",createdOn:"2022-05-17T19:52:23.6846702+00:00"},{id:"121",score:"8",createdOn:"2022-05-19T13:00:00.6846702+00:00"},{id:"122",score:"7",createdOn:"2022-04-11T08:00:00.6846702+00:00"},{id:"121",score:"1",createdOn:"2022-03-12T12:00:00.6846702+00:00"}];
const temp = {};
const language = 'en-GB';
function getMonth(createdOn, language) {
const date = new Date(createdOn);
return new Intl.DateTimeFormat(language, { month: 'long' }).format(date);
}
for (const obj of arr) {
const { id, score, createdOn } = obj;
const month = getMonth(createdOn, language);
const key = `${id}-${month}`;
temp[key] ??= { id, score: 0, month };
temp[key].score += Number(score);
}
console.log(Object.values(temp));
其他文档