我有一個array of objects可以有三種形式的:
第一種形式如下(其中每個只有一條記錄roundId):
[{
"roundId": "b382935b-fe84-4b8c-97e0-eb0b215c68c5",
"teamId": null,
"score": null
},
{
"roundId": "d7f7d3be-cab0-45a6-965f-73f26eb39692",
"teamId": null,
"score": null
},
{
"roundId": "f6aed9f1-5b8a-4da1-aeff-13620aee9c76",
"teamId": null,
"score": null
}
]
而第二種形式可以是這樣的(可以有的地方two records and one record per roundId):
[{
"roundId": "4fc1753a-38ca-4cd4-a772-620d86c35916",
"teamId": "fc6295c4-8527-4329-a3aa-373d854a04c8",
"score": 21
},
{
"roundId": "4fc1753a-38ca-4cd4-a772-620d86c35916",
"teamId": "2e7fcc40-ac45-419b-a80d-a7713a5d6ce0",
"score": 20
},
{
"roundId": "62d0f2a1-be7a-4742-91bc-5a83cf408728",
"teamId": "fc6295c4-8527-4329-a3aa-373d854a04c8",
"score": 18
},
{
"roundId": "62d0f2a1-be7a-4742-91bc-5a83cf408728",
"teamId": "2e7fcc40-ac45-419b-a80d-a7713a5d6ce0",
"score": 7
},
{
"roundId": "d600bdc1-dbd7-4683-9d20-581e4f759ccb",
"teamId": null,
"score": null
}
]
最后一種形式(可以有的地方two records for a roundId):
[{
"roundId": "4fc1753a-38ca-4cd4-a772-620d86c35916",
"teamId": "fc6295c4-8527-4329-a3aa-373d854a04c8",
"score": 21
},
{
"roundId": "4fc1753a-38ca-4cd4-a772-620d86c35916",
"teamId": "2e7fcc40-ac45-419b-a80d-a7713a5d6ce0",
"score": 20
},
{
"roundId": "62d0f2a1-be7a-4742-91bc-5a83cf408728",
"teamId": "fc6295c4-8527-4329-a3aa-373d854a04c8",
"score": 18
},
{
"roundId": "62d0f2a1-be7a-4742-91bc-5a83cf408728",
"teamId": "2e7fcc40-ac45-419b-a80d-a7713a5d6ce0",
"score": 7
},
{
"roundId": "d600bdc1-dbd7-4683-9d20-581e4f759ccb",
"teamId": "fc6295c4-8527-4329-a3aa-373d854a04c8",
"score": 19
},
{
"roundId": "d600bdc1-dbd7-4683-9d20-581e4f759ccb",
"teamId": "2e7fcc40-ac45-419b-a80d-a7713a5d6ce0",
"score": 12
}
]
我需要基于roundId key回傳一個新的物件陣列,其中一個物件由兩個鍵組成:where是一個由roundId和組成的物件陣列。teamsteamsteamdIdscore
如果teamId and score are null物件陣列應該是這樣的:
[
{
"roundId": "b382935b-fe84-4b8c-97e0-eb0b215c68c5",
"teams": []
},
{
"roundId": "d7f7d3be-cab0-45a6-965f-73f26eb39692",
"teams": []
},
{
"roundId": "f6aed9f1-5b8a-4da1-aeff-13620aee9c76",
"teams": []
}
]
如果有teamId,我必須回傳score:roundId
[
{
"roundId": "4fc1753a-38ca-4cd4-a772-620d86c35916",
"teams": [
{ "teamId": "fc6295c4-8527-4329-a3aa-373d854a04c8", "score": 21 },
{ "teamId": "2e7fcc40-ac45-419b-a80d-a7713a5d6ce0", "score": 20 },
},
{
"roundId": "62d0f2a1-be7a-4742-91bc-5a83cf408728",
"teams": [
{ "teamId": "fc6295c4-8527-4329-a3aa-373d854a04c8", "score": 18 },
{ "teamId": "2e7fcc40-ac45-419b-a80d-a7713a5d6ce0", "score": 7 },
},
{
"roundId": "d600bdc1-dbd7-4683-9d20-581e4f759ccb",
"teams": [
{ "teamId": "fc6295c4-8527-4329-a3aa-373d854a04c8", "score": 19 },
{ "teamId": "2e7fcc40-ac45-419b-a80d-a7713a5d6ce0", "score": 12 },
}
]
我嘗試過使用reduce但我弄錯了:
const finalResult = matchesRoundsScore.reduce((result, currentValue) => {
const foundRound = result.find(item => item.roundId === currentValue.roundId)
if (!foundRound) {
result.push({
roundId: currentValue.roundId,
teams: []
})
} else {
result.push({
roundId: currentValue.roundId,
teams: new Array(currentValue.teamId, currentValue.score)
})
}
}, [])
感謝您的時間!如果有不清楚的地方,請告訴我!
uj5u.com熱心網友回復:
這可能是一個選項,Map用于合并相同的記錄roundId
const merge = input => [...input.reduce((m, {roundId, teamId, score}) => {
if (!m.has(roundId)) {
m.set(roundId, []);
}
if (teamId) {
m.get(roundId).push({teamId, score});
}
return m;
}, new Map())].map(([roundId, m]) => ({roundId, teams: [...m]}));
const input = [
{
"roundId": "4fc1753a-38ca-4cd4-a772-620d86c35916",
"teamId": "fc6295c4-8527-4329-a3aa-373d854a04c8",
"score": 21
},
{
"roundId": "4fc1753a-38ca-4cd4-a772-620d86c35916",
"teamId": "2e7fcc40-ac45-419b-a80d-a7713a5d6ce0",
"score": 20
},
{
"roundId": "62d0f2a1-be7a-4742-91bc-5a83cf408728",
"teamId": "fc6295c4-8527-4329-a3aa-373d854a04c8",
"score": 18
},
{
"roundId": "62d0f2a1-be7a-4742-91bc-5a83cf408728",
"teamId": "2e7fcc40-ac45-419b-a80d-a7713a5d6ce0",
"score": 7
},
{
"roundId": "d600bdc1-dbd7-4683-9d20-581e4f759ccb",
"teamId": null,
"score": null
}
];
console.log(merge(input));
uj5u.com熱心網友回復:
以下是實作目標的一種可能方式。
代碼片段
// method to group data by "roundId"
const groupByRounds = arr => (
// implict-return by extracting "values" of the intermediate result object
Object.values(
arr.reduce( // iterate using "reduce" to generate result-object
(acc, {roundId, teamId, score}) => { // de-structure to access props
acc[roundId] = ({ // create/update value for key: "roundId"
roundId,
teams: (acc[roundId]?.teams ?? []).concat((
teamId && score // if "teamId" and "score" are both truthy
? [{ teamId, score }] // add them to the array
: [] // else, it's an empty array
))
})
return acc; // "acc" is the accumulator / agrregator
},
{} // initial value of "acc" is empty object {}
)
)
);
const dataType0 = [{
"roundId": "b382935b-fe84-4b8c-97e0-eb0b215c68c5",
"teamId": null,
"score": null
},
{
"roundId": "d7f7d3be-cab0-45a6-965f-73f26eb39692",
"teamId": null,
"score": null
},
{
"roundId": "f6aed9f1-5b8a-4da1-aeff-13620aee9c76",
"teamId": null,
"score": null
}
];
const dataType1 = [{
"roundId": "4fc1753a-38ca-4cd4-a772-620d86c35916",
"teamId": "fc6295c4-8527-4329-a3aa-373d854a04c8",
"score": 21
},
{
"roundId": "4fc1753a-38ca-4cd4-a772-620d86c35916",
"teamId": "2e7fcc40-ac45-419b-a80d-a7713a5d6ce0",
"score": 20
},
{
"roundId": "62d0f2a1-be7a-4742-91bc-5a83cf408728",
"teamId": "fc6295c4-8527-4329-a3aa-373d854a04c8",
"score": 18
},
{
"roundId": "62d0f2a1-be7a-4742-91bc-5a83cf408728",
"teamId": "2e7fcc40-ac45-419b-a80d-a7713a5d6ce0",
"score": 7
},
{
"roundId": "d600bdc1-dbd7-4683-9d20-581e4f759ccb",
"teamId": null,
"score": null
}
];
const dataType2 = [{
"roundId": "4fc1753a-38ca-4cd4-a772-620d86c35916",
"teamId": "fc6295c4-8527-4329-a3aa-373d854a04c8",
"score": 21
},
{
"roundId": "4fc1753a-38ca-4cd4-a772-620d86c35916",
"teamId": "2e7fcc40-ac45-419b-a80d-a7713a5d6ce0",
"score": 20
},
{
"roundId": "62d0f2a1-be7a-4742-91bc-5a83cf408728",
"teamId": "fc6295c4-8527-4329-a3aa-373d854a04c8",
"score": 18
},
{
"roundId": "62d0f2a1-be7a-4742-91bc-5a83cf408728",
"teamId": "2e7fcc40-ac45-419b-a80d-a7713a5d6ce0",
"score": 7
},
{
"roundId": "d600bdc1-dbd7-4683-9d20-581e4f759ccb",
"teamId": "fc6295c4-8527-4329-a3aa-373d854a04c8",
"score": 19
},
{
"roundId": "d600bdc1-dbd7-4683-9d20-581e4f759ccb",
"teamId": "2e7fcc40-ac45-419b-a80d-a7713a5d6ce0",
"score": 12
}
];
console.log('data of type 0', groupByRounds(dataType0));
console.log('data of type 1', groupByRounds(dataType1));
console.log('data of type 2', groupByRounds(dataType2));
/*
console.log(
'data of type combined',
groupByRounds([
...dataType0, ...dataType1, ...dataType2
])
);
*/
.as-console-wrapper { max-height: 100% !important; top: 0 }
解釋
在上面的代碼段中添加了行內注釋。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/464584.html
標籤:javascript 数组
上一篇:為什么我不能將專案添加到資料庫?
