gpt4 book ai didi

javascript - 按日期排序列表 Immutable JS

转载 作者:行者123 更新时间:2023-11-30 20:39:05 27 4
gpt4 key购买 nike

我有一个带有对象的Immutable JS List。在每个对象中都有一个键 date 和一个 UNIX timestamp

[{someKey: 1, date: 823748943}, {someKey: 2, date: 2389472938}, {someKey: 3, date: 81278734}]

如何按日期对列表进行排序?

最佳答案

Only a select few methods can be used in withMutations including set, push and pop. These methods can be applied directly against a persistent data-structure where other methods like map, filter, sort, and splice will always return new immutable data-structures and never mutate a mutable collection.

var list = Immutable.List.of({someKey: 1, date: 823748943}, {someKey: 2, date: 2389472938}, {someKey: 3, date: 81278734});

var sortedList = list.sort(function(lhs, rhs) {
return lhs.date > rhs.date;
});

console.log(list);
console.log(sortedList);
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.min.js"></script>

关于javascript - 按日期排序列表 Immutable JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49490056/

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