[From] https://segmentfault.com/a/1190000004460234
Lodash 和 Underscore 是非常優秀的當代JavaScript的工具集合框架,它們被前端開發者廣泛地使用。但是,當我們現在是針對現代化瀏覽器進行開發時,很多時候我們利用的Underscore中的方法已經被ES5與ES6所支持了,如果我們希望我們的項目盡可能地減少依賴的話,我們可以根據目標瀏覽器來選擇不用Lodash或者Underscore。
Quick links
_.each
遍歷一系列的元素,並且調用回調處理方程。
Iterates over a list of elements, yielding each in turn to an iteratee function.
// Underscore/Lodash _.each([1, 2, 3], function(value, index) { console.log(value); }); // output: 1 2 3 // Native [1, 2, 3].forEach(function(value, index) { console.log(value); }); // output: 1 2 3
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
✔ | 1.5 ✔ | 9 ✔ | ✔ | ✔ |
_.map
將某個列表中的元素映射到新的列表中。
// Underscore/Lodash var array1 = [1, 2, 3]; var array2 = _.map(array1, function(value, index) { return value*2; }); console.log(array2); // output: [2, 4, 6] // Native var array1 = [1, 2, 3]; var array2 = array1.map(function(value, index) { return value*2; }); console.log(array2); // output: [2, 4, 6]
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
✔ | 1.5 ✔ | 9 ✔ | ✔ | ✔ |
_.every
測試數組的所有元素是否都通過了指定函數的測試。
// Underscore/Lodash function isLargerThanTen(element, index, array) { return element >=10; } var array = [10, 20, 30]; var result = _.every(array, isLargerThanTen); console.log(result); // output: true // Native function isLargerThanTen(element, index, array) { return element >=10; } var array = [10, 20, 30]; var result = array.every(isLargerThanTen); console.log(result); // output: true
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
✔ | 1.5 ✔ | 9 ✔ | ✔ | ✔ |
_.some
判斷序列中是否存在元素滿足給定方程的條件。
// Underscore/Lodash function isLargerThanTen(element, index, array) { return element >=10; } var array = [10, 9, 8]; var result = _.some(array, isLargerThanTen); console.log(result); // output: true // Native function isLargerThanTen(element, index, array) { return element >=10; } var array = [10, 9, 8]; var result = array.some(isLargerThanTen); console.log(result); // output: true
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
✔ | 1.5 ✔ | 9 ✔ | ✔ | ✔ |
_.reduce
接收一個函數作為累加器(accumulator),數組中的每個值(從左到右)開始縮減,最終為一個值。
// Underscore/Lodash var array = [0, 1, 2, 3, 4]; var result = _.reduce(array, function (previousValue, currentValue, currentIndex, array) { return previousValue + currentValue; }); console.log(result); // output: 10 // Native var array = [0, 1, 2, 3, 4]; var result = array.reduce(function (previousValue, currentValue, currentIndex, array) { return previousValue + currentValue; }); console.log(result); // output: 10
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
✔ | 3.0 ✔ | 9 ✔ | 10.5 | 4.0 |
_.reduceRight
接受一個函數作為累加器(accumulator),讓每個值(從右到左,亦即從尾到頭)縮減為一個值。(與 reduce() 的執行方向相反)
// Underscore/Lodash var array = [0, 1, 2, 3, 4]; var result = _.reduceRight(array, function (previousValue, currentValue, currentIndex, array) { return previousValue - currentValue; }); console.log(result); // output: -2 // Native var array = [0, 1, 2, 3, 4]; var result = array.reduceRight(function (previousValue, currentValue, currentIndex, array) { return previousValue - currentValue; }); console.log(result); // output: -2
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
✔ | 3.0 ✔ | 9 ✔ | 10.5 | 4.0 |
_.filter
使用指定的函數測試所有元素,並創建一個包含所有通過測試的元素的新數組。
// Underscore/Lodash function isBigEnough(value) { return value >= 10; } var array = [12, 5, 8, 130, 44]; var filtered = _.filter(array, isBigEnough); console.log(filtered); // output: [12, 130, 44] // Native function isBigEnough(value) { return value >= 10; } var array = [12, 5, 8, 130, 44]; var filtered = array.filter(isBigEnough); console.log(filtered); // output: [12, 130, 44]
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
✔ | 1.5 ✔ | 9 ✔ | ✔ | ✔ |
_.find
返回數組中滿足測試條件的一個元素,如果沒有滿足條件的元素,則返回 undefined。
// Underscore/Lodash var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': true } ]; _.find(users, function(o) { return o.age < 40; }); // output: object for 'barney' // Native var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': true } ]; users.find(function(o) { return o.age < 40; }); // output: object for 'barney'
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
45.0 | 25.0 ✔ | Not supported | Not supported | 7.1 |
_.findIndex
用來查找數組中某指定元素的索引, 如果找不到指定的元素, 則返回 -1.
// Underscore/Lodash var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': true } ]; var index = _.findIndex(users, function(o) { return o.age >= 40; }); console.log(index); // output: 1 // Native var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': true } ]; var index = users.findIndex(function(o) { return o.age >= 40; }); console.log(index); // output: 1
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
45.0 | 25.0 ✔ | Not supported | Not supported | 7.1 |
_.indexOf
返回指定值在字符串對象中首次出現的位置。從 fromIndex 位置開始查找,如果不存在,則返回 -1。
// Underscore/Lodash var array = [2, 9, 9]; var result = _.indexOf(array, 2); console.log(result); // output: 0 // Native var array = [2, 9, 9]; var result = array.indexOf(2); console.log(result); // output: 0
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
✔ | 1.5 ✔ | 9 ✔ | ✔ | ✔ |
_.lastIndexOf
返回指定元素(也即有效的 JavaScript 值或變量)在數組中的最后一個的索引,如果不存在則返回 -1。從數組的后面向前查找,從 fromIndex 處開始。
// Underscore/Lodash var array = [2, 9, 9, 4, 3, 6]; var result = _.lastIndexOf(array, 9); console.log(result); // output: 2 // Native var array = [2, 9, 9, 4, 3, 6]; var result = array.lastIndexOf(9); console.log(result); // output: 2
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
✔ | ✔ | 9 ✔ | ✔ | ✔ |
_.includes
判斷元素是否在列表中
var array = [1, 2, 3]; // Underscore/Lodash - also called with _.contains _.includes(array, 1); // → true // Native var array = [1, 2, 3]; array.includes(1); // → true
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
47✔ | 43 ✔ | Not supported | 34 | 9 |
_.keys
返回某個對象所有可枚舉的鍵名。
// Underscore/Lodash var result = _.keys({one: 1, two: 2, three: 3}); console.log(result); // output: ["one", "two", "three"] // Native var result2 = Object.keys({one: 1, two: 2, three: 3}); console.log(result2); // output: ["one", "two", "three"]
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
5✔ | 4.0 ✔ | 9 | 12 | 5 |
_.size
返回集合大小。
// Underscore/Lodash var result = _.size({one: 1, two: 2, three: 3}); console.log(result); // output: 3 // Native var result2 = Object.keys({one: 1, two: 2, three: 3}).length; console.log(result2); // output: 3
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
5✔ | 4.0 ✔ | 9 | 12 | 5 |
_.isNaN
判斷是否為NaN
// Underscore/Lodash console.log(_.isNaN(NaN)); // output: true // Native console.log(isNaN(NaN)); // output: true // ES6 console.log(Number.isNaN(NaN)); // output: true
MDN:
In comparison to the global
isNaN()
function,Number.isNaN()
doesn't suffer the problem of forcefully converting the parameter to a number. This means it is now safe to pass values that would normally convert toNaN
, but aren't actually the same value asNaN
. This also means that only values of the type number, that are alsoNaN
, return true. Number.isNaN()
Voice from the Lodash author:
Lodash's
_.isNaN
is equiv to ES6Number.isNaN
which is different than the globalisNaN
.
--- jdalton
Browser Support for isNaN
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
✔ | ✔ | ✔ | ✔ | ✔ |
Browser Support for Number.isNaN
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
25 | 15 | Not supported | ✔ | 9 |
_.reverse
Lodash only
將一個序列反向。
// Lodash var array = [1, 2, 3]; console.log(_.reverse(array)); // output: [3, 2, 1] // Native var array = [1, 2, 3]; console.log(array.reverse()); // output: [3, 2, 1]
Voice from the Lodash author:
Lodash's
_.reverse
just callsArray#reverse
and enables composition like_.map(arrays, _.reverse).
It's exposed on _ because previously, like Underscore
, it was only exposed in the chaining syntax.
--- jdalton
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
1.0✔ | 1.0✔ | 5.5✔ | ✔ | ✔ |
_.join
Lodash only
將一個序列變成一個字符串。
// Lodash var result = _.join(['one', 'two', 'three'], '--'); console.log(result); // output: 'one--two--three' // Native var result = ['one', 'two', 'three'].join('--'); console.log(result) // output: 'one--two--three'
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
1.0✔ | 1.0✔ | 5.5✔ | ✔ | ✔ |
_.toUpper
Lodash only
將字符串大寫。
// Lodash var result = _.toUpper('foobar'); console.log(result); // output: 'FOOBAR' // Native var result = 'foobar'.toUpperCase(); console.log(result); // output: 'FOOBAR'
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
✔ | ✔ | ✔ | ✔ | ✔ |
_.toLower
Lodash only
將字符串變為小寫。
// Lodash var result = _.toLower('FOOBAR'); console.log(result); // output: 'foobar' // Native var result = 'FOOBAR'.toLowerCase(); console.log(result); // output: 'foobar'
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
✔ | ✔ | ✔ | ✔ | ✔ |
_.trim
Lodash only
消去字符串起始的空白。
// Lodash var result = _.trim(' abc '); console.log(result); // output: 'abc' // Native var result = ' abc '.trim(); console.log(result); // output: 'abc'
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
5.0✔ | 3.5✔ | 9.0✔ | 10.5✔ | 5.0✔ |
_.repeat
Lodash only
重復創建字符串。
// Lodash var result = _.repeat('abc', 2); // output: 'abcabc' // Native var result = 'abc'.repeat(2); console.log(result); // output: 'abcabc'
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
41✔ | 24✔ | Not supported | Not supported | 9 |
_.after
Note this is an alternative implementation
創建一個在經過了指定計數器之后才會被調用的方程。
var notes = ['profile', 'settings']; // Underscore/Lodash var renderNotes = _.after(notes.length, render); notes.forEach(function(note) { console.log(note); renderNotes(); }); // Native notes.forEach(function(note, index) { console.log(note); if (notes.length === (index + 1)) { render(); } });
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
|
---|---|---|---|---|---|
✔ | ✔ | ✔ | ✔ | ✔ |
Reference
Inspired by: