gpt4 book ai didi

javascript - 使用 underscore.js 检查列表是否在 javascript 中排序

转载 作者:可可西里 更新时间:2023-11-01 02:51:46 26 4
gpt4 key购买 nike

在 javascript (underscore) 中,如何测试数字列表是否已经排序?

最佳答案

您可以使用_.every 来检查所有元素是否有序:

_.every(arr, function(value, index, array) {
// either it is the first element, or otherwise this element should
// not be smaller than the previous element.
// spec requires string conversion
return index === 0 || String(array[index - 1]) <= String(value);
});

关于javascript - 使用 underscore.js 检查列表是否在 javascript 中排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10914982/

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