gpt4 book ai didi

javascript - 函数式 JavaScript : Produce array of unique values

转载 作者:行者123 更新时间:2023-11-28 15:30:46 26 4
gpt4 key购买 nike

使用 JavaScript 中的函数式编程从另一个数组生成唯一值数组的方法是什么?

应该这样做:toUnique([1,1,2,3,4,4]) => [1,2,3,4]

最佳答案

看看uniq Ramda的功能函数式 JavaScript 库。

R.uniq([1, 1, 2, 1]); //=> [1, 2]
R.uniq([{}, {}]); //=> [{}, {}]
R.uniq([1, '1']); //=> [1, '1']

您可以使用库中的函数或查看 source code ...

function uniq(list) {
var idx = -1, len = list.length;
var result = [], item;
while (++idx < len) {
item = list[idx];
if (!_contains(item, result)) {
result[result.length] = item;
}
}
return result;
};

关于javascript - 函数式 JavaScript : Produce array of unique values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27460477/

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