我有以下陣列實體
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);
});
但它沒有用,我不知道如何檢查月份
uj5u.com熱心網友回復:
您可以使用一個相當標準的“分組方式”,使用id_month.
該示例使用reduce()迭代陣列,toLocaleDateString()從 ISO 日期字串和模板文字中檢索月份名稱以創建復合鍵。
score應該在添加之前轉換為數字以避免意外連接,這里使用一元加號 ( )運算子。
最后,我們只取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)
uj5u.com熱心網友回復:
使用臨時物件來保存更新的資訊。
因為您需要分離出具有相同 ID 但在不同月份創建的物件,您可以使用
id-month臨時物件上的鍵來識別它們。遍歷獲取月份名稱的物件陣列,并創建鍵。(我在
Intl.DateTimeFormat這里使用過,因為您可以傳入一個語言字串以從函式中獲得不同的結果 -'es-ES'例如嘗試。)如果臨時物件上不存在具有該鍵的屬性,則將默認物件添加到具有默認物件值的臨時物件,然后增加其
score(確保首先將其強制為數字)。最后得到
Object.valueswhich 將回傳臨時物件中所有這些值的陣列。
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));
附加檔案
解構賦值
為/的
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/477479.html
標籤:javascript 数组
