gpt4 book ai didi

javascript - 循环对象数组并根据键值对的匹配集进行过滤/排序/分组。

转载 作者:行者123 更新时间:2023-12-03 01:48:54 25 4
gpt4 key购买 nike

我有一些这样的数据:

"players": [
{
"name": "Molla Wague",
"position": "Centre-Back",
"jerseyNumber": 13,
"dateOfBirth": "1991-02-21",
"nationality": "Mali",
"contractUntil": "2018-06-30",
"marketValue": null
},
{
"name": "Heurelho Gomes",
"position": "Keeper",
"jerseyNumber": 1,
"dateOfBirth": "1981-02-15",
"nationality": "Brazil",
"contractUntil": "2019-06-30",
"marketValue": null
},
{
"name": "Christian Kabasele",
"position": "Centre-Back",
"jerseyNumber": 27,
"dateOfBirth": "1991-02-24",
"nationality": "Belgium",
"contractUntil": "2021-06-30",
"marketValue": null
},
{
"name": "José Holebas",
"position": "Left-Back",
"jerseyNumber": 25,
"dateOfBirth": "1984-06-27",
"nationality": "Greece",
"contractUntil": "2020-06-30",
"marketValue": null
},
{
"name": "Daryl Janmaat",
"position": "Right-Back",
"jerseyNumber": 2,
"dateOfBirth": "1989-07-22",
"nationality": "Netherlands",
"contractUntil": "2020-06-30",
"marketValue": null
},
{
"name": "Étienne Capoue",
"position": "Defensive Midfield",
"jerseyNumber": 29,
"dateOfBirth": "1988-07-11",
"nationality": "France",
"contractUntil": "2019-06-30",
"marketValue": null
},
{
"name": "Tom Cleverley",
"position": "Central Midfield",
"jerseyNumber": 8,
"dateOfBirth": "1989-08-12",
"nationality": "England",
"contractUntil": "2022-06-30",
"marketValue": null
},
{
"name": "Roberto Pereyra",
"position": "Attacking Midfield",
"jerseyNumber": 37,
"dateOfBirth": "1991-01-07",
"nationality": "Argentina",
"contractUntil": "2021-06-30",
"marketValue": null
},
{
"name": "Troy Deeney",
"position": "Centre-Forward",
"jerseyNumber": 9,
"dateOfBirth": "1988-06-29",
"nationality": "England",
"contractUntil": "2021-06-30",
"marketValue": null
}
]

有些玩家有不同的位置

我想按每个球员的位置对他们进行排序,所以所有守门员都排在第一位,然后是所有中后卫,依此类推。

我正在使用 React 和 Redux。我已经在我的Reducer文件中完成了一些工作,但我认为代码太臃肿,下面是一个示例:

function sortPlayersByPosition(data) {
let players = data.players;
let result = [];

result.push(players.filter(function(obj) {
return obj.position === 'Keeper';
}));

result.push(players.filter(function(obj) {
return obj.position === 'Centre-Back';
}));

result.push(players.filter(function(obj) {
return obj.position === 'Right-Back';
}));

result.push(players.filter(function(obj) {
return obj.position === 'Left-Back';
}));

result.push(players.filter(function(obj) {
return obj.position === 'Defensive Midfield';
}));

result.push(players.filter(function(obj) {
return obj.position === 'Central Midfield';
}));

result.push(players.filter(function(obj) {
return obj.position === 'Attacking Midfield';
}));

result.push(players.filter(function(obj) {
return obj.position === 'Right Wing';
}));

result.push(players.filter(function(obj) {
return obj.position === 'Left Wing';
}));

result.push(players.filter(function(obj) {
return obj.position === 'Centre-Forward';
}));

data.players = result;

return data
}

export const fetchTeamPlayersReducer = (state = [], action) => {
switch (action.type) {
case 'FETCH_TEAM_PLAYERS_SUCCESS':

var sortedPlayers = sortPlayersByPosition(action.payload);
return sortedPlayers;
default:
return state;
}
}

Click here to see output of the code above

正如你所看到的,我已经将我的位置分成了单独的数组。我不希望这个结果。如果我有一个按顺序排列对象的数组,我真的很喜欢它。就像原来的格式一样。

欢迎任何帮助。

谢谢!

最佳答案

您可以首先制作有序位置数组:

const ORDERS = [
'Keeper',
'Centre-Back',
...
]

并按此 ORDERS 数组对初始数组进行排序

players.sort((player1, player2) => {
return ORDERS.indexOf(player2.position) - ORDERS.indexOf(player1.position);
});

关于javascript - 循环对象数组并根据键值对的匹配集进行过滤/排序/分组。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50508883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com