Underscore.JS — uniq 方法



語法

_.uniq(array, [isSorted], [iteratee])

uniq 方法從陣列中返回一個唯一值的陣列。它使用型別安全相等性 (===) 檢查。如果陣列已排序,請傳遞 true 來執行更快的演算法。可傳遞一個迭代器函式來確定專案的唯一性。

示例

var _ = require('underscore');

var list1 = [11, 2, 14, 14, 2, 6]
var list2 = [1, 2, 3, 3, 3, 4, 5]
//Example 1: uniq values
result = _.uniq(list1);
console.log(result)

//Example 2: uniq values of an sorted array
result = _.uniq(list2, true);
console.log(result)

將上述程式儲存在 tester.js 中。執行以下命令來執行此程式。

命令

\>node tester.js

輸出

[ 11, 2, 14, 6 ]
[ 1, 2, 3, 4, 5 ]
underscorejs_processing_array.htm
廣告
© . All rights reserved.