Lodash _.sortedUniqBy() 方法
_.sortedUniqBy 方法用于返回可以插入元素的数组的最低索引并保持其排序顺序。此外,此方法与 _.uniqBy 类似,只是它是为排序数组设计和优化的。
句法:
_.sortedUniqBy(array, [iteratee])
参数:此方法接受上面提到的两个参数,如下所述:
- array:此参数保存要检查的数组。
- [iteratee=_.identity] (函数):此参数保存每个元素调用的迭代对象。
返回值:该方法用于返回新的重复空闲数组。
示例 1:这里使用 const _ = require('lodash') 将 lodash 库导入文件。
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let y = ([1.1, 1.2, 2.1, 2.3, 2.4, 3.5])
// Use of _.sortedUniqBy()
// method
let index = _.sortedUniqBy(y, Math.floor);
// Printing the output
console.log(index);
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let y = ([112.1, 112.2, 122.1, 122.3, 122.4, 132.5])
// Use of _.sortedUniqBy()
// method
let index = _.sortedUniqBy(y, Math.floor);
// Printing the output
console.log(index);
输出:
[1.1, 2.1, 3.5]
示例 2:
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let y = ([112.1, 112.2, 122.1, 122.3, 122.4, 132.5])
// Use of _.sortedUniqBy()
// method
let index = _.sortedUniqBy(y, Math.floor);
// Printing the output
console.log(index);
输出:
[112.1, 112.1, 132.5]
注意:这在普通 JavaScript 中不起作用,因为它需要安装库 lodash。