Underscore.JS - uniq 方法
语法
_.uniq(array, [isSorted], [iteratee])
uniq 方法从数组中返回一个包含唯一值的数组。它使用类型安全的 equaliy(===) 检查。如果数组已排序,则传递 true 以运行更快的算法。可以传递 iteratee 函数来确定项目的唯一性。
示例
var _ = require('underscore'); var list1 = [11, 2, 14, 14, 2, 6] var list2 = [1, 2, 3, 3, 3, 4, 5] //示例 1:uniq 值 result = _.uniq(list1); console.log(result) //示例 2:已排序数组的 uniq 值 result = _.uniq(list2, true); console.log(result)
将上述程序保存在tester.js中。运行以下命令执行该程序。
命令
\>node tester.js
输出
[ 11, 2, 14, 6 ] [ 1, 2, 3, 4, 5 ]
underscorejs_processing_array.html