gpt4 book ai didi

javascript - 使用 JavaScript 对 JSON 数据进行排序

转载 作者:行者123 更新时间:2023-12-03 11:59:44 26 4
gpt4 key购买 nike

我正在寻找订购来自 JSON 提供程序的数据的方法:

[{"Username":"Mehmet","UserID":2,"OkeyTablePlayerChairNumber":1},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":2},
{"Username":"Erçin","UserID":1,"OkeyTablePlayerChairNumber":3},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":4},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":5},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":6},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":7},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":8},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":9}]

订购还有两点比较重要:

  1. OkeyTablePlayerChairNumber
  2. 首先按用户ID;让我解释一下:

我想先按 OkeyTableChairNumber 订购;这已经由服务器端完成了。数据按 OkeyTablePlayerChairNumber ASC 排序...

现在公牛,我想通过用户 ID 订购它们,但是;例如,如果我采用 UserID == 1,则应将它们排序为: UserID == 1 字段,将出现在第一个字段。上面的对象将被删除并添加到列表的末尾...

查看:

用户 ID == 1

 [{"Username":"Erçin","UserID":1,"OkeyTablePlayerChairNumber":3},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":4},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":5},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":6},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":7},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":8},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":9},
{"Username":"Mehmet","UserID":2,"OkeyTablePlayerChairNumber":1},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":2}]

我之前做过的C#解决方案;但我需要 JS 解决方案:

int yourSitPositionIndex = playersOnTheTableWithEmpytPositions.ToList().FindIndex(x => x.UserID == userID);
var beforePlayers = playersOnTheTableWithEmpytPositions.ToList().GetRange(0, yourSitPositionIndex);
IEnumerable<tbl_Okey_TablePlayer> afterPlayers = playersOnTheTableWithEmpytPositions.Except(beforePlayers);
IEnumerable<tbl_Okey_TablePlayer> newPositions = afterPlayers.Concat(beforePlayers);

最佳答案

C# 解决方案的转换:

var wantedId = 1;

var index = 0, result;
//int yourSitPositionIndex = playersOnTheTableWithEmpytPositions.ToList().FindIndex(x => x.UserID == userID);
while (index < data.length && data[index].UserID != wantedId) index += 1;
if (index < data.length) {
//var beforePlayers = playersOnTheTableWithEmpytPositions.ToList().GetRange(0, yourSitPositionIndex);
var beforePlayers = data.slice(0, index);
//IEnumerable<tbl_Okey_TablePlayer> afterPlayers = playersOnTheTableWithEmpytPositions.Except(beforePlayers);
var afterPlayers = data.slice(index);
//IEnumerable<tbl_Okey_TablePlayer> newPositions = afterPlayers.Concat(beforePlayers);
result = afterPlayers.concat(beforePlayers);
} else {
result = data;
}
  • javascript核心中没有FindIndex,所以我做了一个while循环来查找索引
  • 我使用.slice()替换 GetRange
  • 其余代码应该足够清晰。所有代码都可以使用 underscore 这样的库来简化

Example fiddle

关于javascript - 使用 JavaScript 对 JSON 数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25466488/

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