_.uniq方法用於從所有給定數組(使用SameValueZero進行相等性比較)按順序創建唯一值的數組。
用法:
_.union(array)
參數:此方法接受如上所述和以下描述的單個參數:
- array:此參數保存要檢查的數組。
返回值:此方法用於返回合並值的新數組。
範例1:在這裏,const _ = require(‘lodash’)用於將lodash庫導入文件中。
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let y = ([1, 2, 2, 3, 4, 3, 8, 6]);
// Use of _.uniq()
// method
let gfg = _.uniq(y);
// Printing the output
console.log(gfg);
輸出:
[ 1, 2, 3, 4, 8, 6 ]
範例2:
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let y = (['a', 'b', 'c', 'e', 'd', 'd', 'g', 'i', 'i']);
// Use of _.uniq()
// method
let gfg = _.uniq(y);
// Printing the output
console.log(gfg);
輸出:
[ 'a', 'b', 'c', 'e', 'd', 'g', 'i' ]
範例3:
javascript
// Requiring the lodash library
const _ = require("lodash");
// Original array
let y = (['apple', 'banana', 'banana', 'chikoo',
'Elderberry', 'Damson
Date', 'guava', 'Damson Date']);
// Use of _.uniq()
// method
let gfg = _.uniq(y);
// Printing the output
console.log(gfg);
輸出:
['apple', 'banana', 'chikoo', 'Elderberry', 'Damson Date', 'guava']
注意:該代碼在常規JavaScript中不起作用,因為它需要安裝庫lodash。
相關用法
- underscore.js _.uniq()用法及代碼示例
- Lodash _.nth()用法及代碼示例
- Lodash _.add()用法及代碼示例
- Lodash _.last()用法及代碼示例
- Lodash _.xor()用法及代碼示例
- Lodash _.every()用法及代碼示例
- Lodash _.take()用法及代碼示例
- Lodash _.min()用法及代碼示例
- Lodash _.max()用法及代碼示例
- Lodash _.zip()用法及代碼示例
- Lodash _.takeRight()用法及代碼示例
- Lodash _.maxBy()用法及代碼示例
- Lodash _.minBy()用法及代碼示例
- Lodash _.isEqual()用法及代碼示例
- Lodash _.meanBy()用法及代碼示例
- Lodash _.pullAt()用法及代碼示例
- Lodash _.xorBy()用法及代碼示例
- Lodash _.zipObjectDeep()用法及代碼示例
- Lodash _.findLast()用法及代碼示例
- Lodash _.groupBy()用法及代碼示例
注:本文由純淨天空篩選整理自shivanisinghss2110大神的英文原創作品 Lodash _.uniq() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。