我正在呼叫 API 并獲取資料。
0: {team: {…}, league: {…}, games: {…}, substitutes: {…}, shots: {…}, …}
1: {team: {…}, league: {…}, games: {…}, substitutes: {…}, shots: {…}, …}
2: {team: {…}, league: {…}, games: {…}, substitutes: {…}, shots: {…}, …}
3: {team: {…}, league: {…}, games: {…}, substitutes: {…}, shots: {…}, …}
4: {team: {…}, league: {…}, games: {…}, substitutes: {…}, shots: {…}, …}
現在我想做的是映射這個物件陣列,并在一個物件陣列中使用相同的團隊過濾它,而在另一個物件陣列中使用其他團隊。
0:
cards: {yellow: 2, yellowred: 0, red: 0}
dribbles: {attempts: 9, success: 5, past: null}
duels: {total: 113, won: 58}
fouls: {drawn: 9, committed: 16}
games: {appearences: 23, lineups: 11, minutes: 1007, number: null, position: 'Attacker', …}
goals: {total: 8, conceded: 0, assists: 3, saves: null}
league: {id: 135, name: 'Serie A', country: 'Italy', logo: 'https://media.api-sports.io/football/leagues/135.png', flag: 'https://media.api-sports.io/flags/it.svg', …}
passes: {total: 414, key: 23, accuracy: 13}
penalty: {won: null, commited: null, scored: 0, missed: 1, saved: null}
shots: {total: 42, on: 20}
substitutes: {in: 12, out: 4, bench: 14}
tackles: {total: null, blocks: 1, interceptions: 3}
team: {id: 489, name: 'AC Milan', logo: 'https://media.api-sports.io/football/teams/489.png'}
[[Prototype]]: Object
每個團隊都有不同的 ID。而且由于我事先不知道團隊的ID,因此我不知道如何過濾它。一切都是動態的,即使是 id。
uj5u.com熱心網友回復:
var _ = require('lodash');
list = [
{check: 1 , team: {id: 489, name: 'AC Milan'} }, { check: 1 , team: {id: 489, name: 'AC Milan1'} },
{ check: 1 , team: {id: 489, name: 'AC Milan1'} }, { check: 2 , team: {id: 489, name: 'AC Milan3'}},
{ check: 2 , team: {id: 489, name: 'AC Milan2'}}, { check: 3 , team: {id: 489, name: 'AC Milan5'}},
{ check: 5 , team: {id: 489, name: 'AC Milan3'}}, { check: 5 , team: {id: 489, name: 'AC Milan7'} }
]
test = _.groupBy(list, "team.name");
console.log(test)
uj5u.com熱心網友回復:
以下是我如何分解這些型別的問題。
- 是減少還是映射?當結果可以/必須鍵入時,前者為真。
- 什么喂食(1)?
我的第一個猜測是你有一組團隊提供的減少。
歸約是一個類似于 (array, {}) -> { key: value } 的函式
該陣列具有創建鍵和值的資訊。
陣列:原始資料
keyFromRawFn:
rawData => rawData.team.id
valueFromRawFn:
rawData => a record as described in your result
所以,減少將是,
rawDatas.reduction((newObj, rawData) => {
const key = keyFromRawFn(rawData);
const value = valueFromRawFn(rawData);
newObj[key] = value;
return newObj;
}, {});
這假定 rawDatas 的陣列長度 = 團隊數。似乎是一個合理的第一個猜測,因為每個“團隊”都有“游戲”。
如果不是,這仍然是一個有效的“核心”例程。我們需要找出一個“預處理”來獲得滿足標準的 rawDatas。該預處理將是一個以 id 作為鍵的縮減,值另一個縮減,它通過 preRawDatas 條目組合每個 preRawData(如果計數使用 sum,或者如果它們都具有游戲數,則使用平均值,或者 game_count x 平均值的總和)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/481249.html
標籤:javascript 数组 反应 目的 筛选
