gpt4 book ai didi

javascript - JS排序不是函数

转载 作者:行者123 更新时间:2023-11-30 09:27:18 25 4
gpt4 key购买 nike

虽然我遇到了data.sort is not a function错误,但我目前正在尝试对对象进行排序。

我从以下 StackOverflow 答案中获得灵感:Sort Table by click in header tag regardless of it is numeric, alphabetical or date .

我也浏览了其他问题,但还没有设法解决我的问题,因为它往往会导致其他问题,例如值 undefined

这是我的对象:

{
"2010-01": {
"item1": 324,
"item2": 1075,
"item3": 940,
"item4": 441,
"item5": 1040,
"item6": 898,
"item7": 1343
},
"2011-02": {
"item1": 295,
"item2": 958,
"item3": 904,
"item4": 434,
"item5": 1038,
"item6": 793,
"item7": 1246
},
"2012-03": {
"item1": 314,
"item2": 1062,
"item3": 980,
"item4": 494,
"item5": 1158,
"item6": 914,
"item7": 1461
},
"2008-04": {
"item1": 336,
"item2": 1022,
"item3": 987,
"item4": 488,
"item5": 1014,
"item6": 792,
"item7": 1382
},
"2007-05": {
"item1": 332,
"item2": 1073,
"item3": 1002,
"item4": 512,
"item5": 1104,
"item6": 840,
"item7": 1368
},
"2005-06": {
"item1": 311,
"item2": 981,
"item3": 837,
"item4": 432,
"item5": 1002,
"item6": 801,
"item7": 1265
},
"2014-07": {
"item1": 321,
"item2": 1049,
"item3": 921,
"item4": 489,
"item5": 963,
"item6": 881,
"item7": 1340
},
"2015-08": {
"item1": 294,
"item2": 1071,
"item3": 960,
"item4": 506,
"item5": 910,
"item6": 885,
"item7": 1312
},
"2016-09": {
"item1": 281,
"item2": 1020,
"item3": 952,
"item4": 502,
"item5": 1068,
"item6": 914,
"item7": 1397
},
"2009-10": {
"item1": 319,
"item2": 1058,
"item3": 985,
"item4": 546,
"item5": 1184,
"item6": 1031,
"item7": 1448
},
"2005-11": {
"item1": 300,
"item2": 1021,
"item3": 967,
"item4": 474,
"item5": 1176,
"item6": 1009,
"item7": 1387
},
"2017-12": {
"item1": 307,
"item2": 1027,
"item3": 924,
"item4": 427,
"item5": 1024,
"item6": 844,
"item7": 1300
}
}

我的排序功能,目前我只是尝试按升序或降序对第一项进行排序。

function sortData(key, data, type) {
if (key === "item1") {
if (type === "asc") {
data.sort(function(a, b) {
return a[key] > b[key];
});
} else {
data.sort(function(a, b) {
return a[key] < b[key];
});
}
}
return data;
}

data 是对象,我试过像这样传递它:sortData('item1', [data], 'asc'); 作为反对只是 data 但没有运气。如果有人可以阐明我做错了什么,那就太好了。

最佳答案

问题是数据不是数组,排序是数组方法。您可以使用 Object.keys 获取键数组,对其进行排序,然后使用 forEach 填充值:

let unsorted_data = { "2010-01": { "item1": 324, "item2": 1075, "item3": 940, "item4": 441, "item5": 1040, "item6": 898, "item7": 1343 }, "2011-02": { "item1": 295, "item2": 958, "item3": 904, "item4": 434, "item5": 1038, "item6": 793, "item7": 1246 }, "2012-03": { "item1": 314, "item2": 1062, "item3": 980, "item4": 494, "item5": 1158, "item6": 914, "item7": 1461 }, "2008-04": { "item1": 336, "item2": 1022, "item3": 987, "item4": 488, "item5": 1014, "item6": 792, "item7": 1382 }, "2007-05": { "item1": 332, "item2": 1073, "item3": 1002, "item4": 512, "item5": 1104, "item6": 840, "item7": 1368 }, "2005-06": { "item1": 311, "item2": 981, "item3": 837, "item4": 432, "item5": 1002, "item6": 801, "item7": 1265 }, "2014-07": { "item1": 321, "item2": 1049, "item3": 921, "item4": 489, "item5": 963, "item6": 881, "item7": 1340 }, "2015-08": { "item1": 294, "item2": 1071, "item3": 960, "item4": 506, "item5": 910, "item6": 885, "item7": 1312 }, "2016-09": { "item1": 281, "item2": 1020, "item3": 952, "item4": 502, "item5": 1068, "item6": 914, "item7": 1397 }, "2009-10": { "item1": 319, "item2": 1058, "item3": 985, "item4": 546, "item5": 1184, "item6": 1031, "item7": 1448 }, "2005-11": { "item1": 300, "item2": 1021, "item3": 967, "item4": 474, "item5": 1176, "item6": 1009, "item7": 1387 }, "2017-12": { "item1": 307, "item2": 1027, "item3": 924, "item4": 427, "item5": 1024, "item6": 844, "item7": 1300 } };

function sortData(key, data, type) {
let ordered = {};
let compareFunction = function(a, b) {
return data[b][key] - data[a][key];
};
if (type === "asc") {
compareFunction = function(a, b) {
return data[a][key] - data[b][key];
}
}
Object.keys(data).sort(compareFunction).forEach(function(key) {
ordered[key] = data[key];
});
return ordered;
}


console.log(sortData("item1", unsorted_data, 'asc'));

您仍然可以为 asc 和 desc 传递自定义比较函数。顺序 sort,但为了简单起见,我省略了它。

关于javascript - JS排序不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48604226/

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