從下面的 JSON 中,我想獲取玩家并列出他們被參考的次數。因此,例如球員 3,切爾西被參考一次,是排名第一的頂級球員,那么吉姆被參考兩次將是排名第二,然后球員 1,喬,將是串列的最后一名,因為他們被參考了 3 次. 所以從最少參考到最多。
我正在考慮通過 JS 使用陣列過濾器方法,但不確定這是否可行?有沒有辦法讓我使用過濾器將陣列中的一個物件與另一個物件進行比較?
我在想像下面這樣的 JS,for 回圈的第一部分,會將每個玩家與陣列中的下一個玩家進行比較,但它不會準確地比較它們,因為它會過濾每個回圈,所以我的影像會破壞性能和過濾不準確。這就是為什么我還包括 if 條件我不知道這是否會比 filter 更好?
let topPlayers = []
for(let i=0; i<players.length; i ) {
players.filter((player) => player[i] == player[i 1])
if(players[i].title == players[i 1].title) {
topPlayers.push(players[i])
}
}
const players = [
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player two",
"subtitles": [
{
"name": "Jim"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player two",
"subtitles": [
{
"name": "Jim"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player three",
"subtitles": [
{
"name": "Chelsea"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"PC"
]
},
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
}
]
uj5u.com熱心網友回復:
一種方法是結合.reduce(..)和.sort(..)。
這是一個例子:
const players = [
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player two",
"subtitles": [
{
"name": "Jim"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player two",
"subtitles": [
{
"name": "Jim"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
},
{
"header": "Player",
"title": "Player three",
"subtitles": [
{
"name": "Chelsea"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"PC"
]
},
{
"header": "Player",
"title": "Player one",
"subtitles": [
{
"name": "Joe"
}
],
"time": "2016-11-08T16:03:08.957Z",
"products": [
"Xbox"
]
}
]
const result = players.reduce((a, c) => {
const found = a.find((o) => o.title === c.title);
if (found) {
found.count ;
return a;
}
a.push({ title: c.title, subtitles: c.subtitles, count: 1 });
return a;
}, []).sort((a, b) => a.count - b.count);
console.log(result.map((o, i) => `Player: ${o.subtitles[0].name} is #${i 1}`));
這樣做是首先將陣列簡化為僅包含唯一條目(原始物件的簡化版本)和每個物件的出現次數(count屬性)的陣列。然后,它根據這個數字對陣列進行排序。
uj5u.com熱心網友回復:
您可以使用另一個物件,該物件將為每個唯一的播放器標題保存一個屬性,然后每次遇到相同的標題時增加此屬性的值:
const counter = {};
for(const player in players) {
// checks whether property named as player already exists
if(!counter[player.title]) {
// Create new property, named as player, with 1 as value
counter[player.title] = 1;
} else {
// Increment property value
counter[player.title] ;
}
}
for(const player in counter) {
console.log(`The player ${player} is recorded ${counter[counter]} times`);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/477194.html
標籤:javascript json
