Lodash - uniqBy 方法
语法
_.uniqBy(array, [iteratee=_.identity])
此方法与 _.uniq 类似,不同之处在于它接受 iteratee,后者针对数组中的每个元素调用,以生成计算唯一性的标准。结果值的顺序由它们在数组中出现的顺序决定。iteratee 使用一个参数调用:(value)。
参数
array (Array) − 要检查的数组。
[iteratee=_.identity] (Function) −每个元素调用的迭代器。
输出
(Array) − 返回新的重复自由数组。
示例
var _ = require('lodash'); var numbers = [1.1, 2.1, 2.3, 4.2] var result = _.uniqBy(numbers, Math.floor); console.log(result);
将上述程序保存在tester.js中。运行以下命令执行该程序。
命令
\>node tester.js
输出
[ 1.1, 2.1, 4.2 ]