gpt4 book ai didi

javascript - 如何对javascript数组进行排序

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

我想按时间戳对数组进行排序,但出现未排序的错误

var userValue = {
timestamp,
username,
token,
userid,
message
};
userArray.push(userValue);

这是我得到的数组

enter image description here

userArray.sort(function(a, b) {
return a[0]['timestamp'] - b[0]['timestamp'];
});

我想根据时间戳对数组进行排序

最佳答案

这段代码

userArray[childDatamsg.timestamp] = userValue;

timestamp 索引处添加一个对象。这就是为什么你有一个 length1563533788 的数组!而是将 userValue 对象 pushuserArray

userArray.push(userValue);

现在,它将具有从 0 开始的索引。

然后你可以像这样对数组进行排序:

userArray.sort((a, b) => a.timestamp - b.timestamp)

关于javascript - 如何对javascript数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57111233/

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