gpt4 book ai didi

javascript - 按字母顺序对 JavaScript 中的哈希表进行排序

转载 作者:行者123 更新时间:2023-12-02 18:38:54 25 4
gpt4 key购买 nike

我正在尝试在 JavaScript 中按字母顺序对哈希表(最初称为“resultVal”)进行排序。

// initializing an array with all the keys. //
var keys = [];
// populating it with all the keys in the hashtable. //
for (var key in resultVal) {
if (resultVal.hasOwnProperty(key)) {
keys.push(key);
}
}
// Alphabetically sorting the array populated with hash table keys. //
keys.sort();
var temp = {};

for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = resultVal[key];
if (key != "") {
temp[key].push(value);
}
}

我的问题在于最后一条语句:-

temp[key].push(value);

我正在做的是,按字母顺序对键进行排序,并将键及其各自的值重新输入临时哈希表...“temp”。

push 语句未被识别。有人可以帮忙吗?

最佳答案

temp 被定义为对象,而不是数组。无需 push() 到它上面:

temp[key] = value;

关于javascript - 按字母顺序对 JavaScript 中的哈希表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17030295/

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