項目中使用過的lodash方法總結

_.findWhere

對集合中的每個元素和源進行深度比較。

該方法支持比較數組、布爾值、數字、日期對象,對象的對象,正則表達式、字符串。

Ex:

var users = [

  { 'user': 'barney', 'age': 36, 'active': true },

  { 'user': 'fred',   'age': 40, 'active': false }

];


_.result(_.findWhere(users, { 'age': 36, 'active': true }), 'user');

// => 'barney'


_.result(_.findWhere(users, { 'age': 40, 'active': false }), 'user');

// => 'fred'


_.pluck

從元素集合中獲取聲明的屬性的值構成的數組

Ex:

var users = [

  { 'user': 'barney', 'age': 36 },

  { 'user': 'fred',   'age': 40 }

];


_.pluck(users, 'user');

// => ['barney', 'fred']


_.contains

檢查 value(值) 是否在 collection(集合) 中。如果 collection(集合)是一個字符串,那麼檢查 value(值,子字符串) 是否在字符串中, 否則使用 SameValueZero 做等值比較。 如果指定 fromIndex 是負數,那麼從 collection(集合) 的結尾開始檢索。

Ex:

_.includes([1, 2, 3], 1);

// => true

 

_.includes([1, 2, 3], 1, 2);

// => false

 

_.includes({ 'user': 'fred', 'age': 40 }, 'fred');

// => true

 

_.includes('pebbles', 'eb');

// => true


_.difference

創建一個具有唯一array值的數組,每個值不包含在其他給定的數組中。(愚人碼頭注:即創建一個新數組,這個數組中的值,爲第一個數字(array 參數)排除了給定數組中的值。)該方法使用 SameValueZero做相等比較。結果值的順序是由第一個數組中的順序確定。

Ex:

_.difference([3, 2, 1], [4, 2]);

// => [3, 1]


_.filter

遍歷 collection(集合)元素,返回 predicate(斷言函數)返回真值 的所有元素的數組。 predicate(斷言函數)調用三個參數:(value, index|key, collection)。

Ex:

var users = [

  { 'user': 'barney', 'age': 36, 'active': true },

  { 'user': 'fred',   'age': 40, 'active': false }

];

 

_.filter(users, function(o) { return !o.active; });

// => objects for ['fred']

 

// The `_.matches` iteratee shorthand.

_.filter(users, { 'age': 36, 'active': true });

// => objects for ['barney']

 

// The `_.matchesProperty` iteratee shorthand.

_.filter(users, ['active', false]);

// => objects for ['fred']

 

// The `_.property` iteratee shorthand.

_.filter(users, 'active');

// => objects for ['barney']


_.isEmpty

檢查 value 是否爲一個空對象,集合,映射或者set。 判斷的依據是除非是有枚舉屬性的對象,length 大於 0 的 arguments object, array, string 或類jquery選擇器。 


對象如果被認爲爲空,那麼他們沒有自己的可枚舉屬性的對象。 


類數組值,比如arguments對象,array,buffer,string或者類jQuery集合的length 爲 0,被認爲是空。類似的,map(映射)和set 的size 爲 0,被認爲是空。

Ex:

_.isEmpty(null);

// => true

 

_.isEmpty(true);

// => true

 

_.isEmpty(1);

// => true

 

_.isEmpty([1, 2, 3]);

// => false

 

_.isEmpty({ 'a': 1 });

// => false


_.result

這個方法類似 _.get, 除了如果解析到的值是一個函數的話,就綁定 this 到這個函數並返回執行後的結果。

Ex:

var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] };

 

_.result(object, 'a[0].b.c1');

// => 3

 

_.result(object, 'a[0].b.c2');

// => 4

 

_.result(object, 'a[0].b.c3', 'default');

// => 'default'

 

_.result(object, 'a[0].b.c3', _.constant('default'));

// => 'default'


_.some

通過 predicate(斷言函數) 檢查collection(集合)中的元素是否存在 任意 truthy(真值)的元素,一旦 predicate(斷言函數) 返回 truthy(真值),遍歷就停止。 predicate 調用3個參數:(value, index|key, collection)。

Ex:

_.some([null, 0, 'yes', false], Boolean);

// => true

 

var users = [

  { 'user': 'barney', 'active': true },

  { 'user': 'fred',   'active': false }

];

 

// The `_.matches` iteratee shorthand.

_.some(users, { 'user': 'barney', 'active': false });

// => false

 

// The `_.matchesProperty` iteratee shorthand.

_.some(users, ['active', false]);

// => true

 

// The `_.property` iteratee shorthand.

_.some(users, 'active');

// => true


_.values

創建 object 自身可枚舉屬性的值爲數組。 


注意: 非對象的值會強制轉換爲對象。

Ex:

function Foo() {

  this.a = 1;

  this.b = 2;

}

 

Foo.prototype.c = 3;

 

_.values(new Foo);

// => [1, 2] (無法保證遍歷的順序)

 

_.values('hi');

// => ['h', 'i']

_.omit

刪除object對象的屬性。

Ex:

var object = { 'a': 1, 'b': '2', 'c': 3 };

 

_.omit(object, ['a', 'c']);

// => { 'b': '2' }


_.isObject

檢查 value 是否爲 Object 的 language type。 (例如: arrays, functions, objects, regexes,new Number(0), 以及 new String(''))

Ex:

_.isObject({});

// => true

 

_.isObject([1, 2, 3]);

// => true

 

_.isObject(_.noop);

// => true

 

_.isObject(null);

// => false


_.extend

分配來源對象的可枚舉屬性到目標對象上。 來源對象的應用規則是從左到右,隨後的下一個對象的屬性會覆蓋上一個對象的屬性。

他會遍歷並繼承來源對象上的屬性

Ex:

function Foo() {

  this.a = 1;

}

 

function Bar() {

  this.c = 3;

}

 

Foo.prototype.b = 2;

Bar.prototype.d = 4;

 

_.extend({ 'a': 0 }, new Foo, new Bar);

// => { 'a': 1, 'b': 2, 'c': 3, 'd': 4 }


_.where

對集合和源對象中的每個元素進行深度比較,返回具有相同屬性值的所有元素的數組。

Ex:

var users = [

  { 'user': 'barney', 'age': 36, 'active': false, 'pets': ['hoppy'] },

  { 'user': 'fred',   'age': 40, 'active': true, 'pets': ['baby puss', 'dino'] }

];


_.pluck(_.where(users, { 'age': 36, 'active': false }), 'user');

// => ['barney']


_.pluck(_.where(users, { 'pets': ['dino'] }), 'user');

// => ['fred']


_.sortBy

創建一個元素數組。 以 iteratee 處理的結果升序排序。 這個方法執行穩定排序,也就是說相同元素會保持原始排序。 iteratees 調用1個參數: (value)。

Ex:

var users = [

  { 'user': 'fred',   'age': 48 },

  { 'user': 'barney', 'age': 36 },

  { 'user': 'fred',   'age': 40 },

  { 'user': 'barney', 'age': 34 }

];

 

_.sortBy(users, function(o) { return o.user; });

// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]

 

_.sortBy(users, ['user', 'age']);

// => objects for [['barney', 34], ['barney', 36], ['fred', 40], ['fred', 48]]

 

_.sortBy(users, 'user', function(o) {

  return Math.floor(o.age / 10);

});

// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]


_.has

檢查 path 是否是object對象的直接屬性。

Ex:

var object = { 'a': { 'b': 2 } };

var other = _.create({ 'a': _.create({ 'b': 2 }) });

 

_.has(object, 'a');

// => true

 

_.has(object, 'a.b');

// => true

 

_.has(object, ['a', 'b']);

// => true

 

_.has(other, 'a');

// => false


_.each

調用 iteratee 遍歷 collection(集合) 中的每個元素, iteratee 調用3個參數: (value, index|key, collection)。 如果迭代函數(iteratee)顯式的返回 false ,迭代會提前退出。 


注意: 與其他"集合"方法一樣,類似於數組,對象的 "length" 屬性也會被遍歷。想避免這種情況,可以用 _.forIn 或者 _.forOwn 代替。

Ex:

_([1, 2]).forEach(function(value) {

  console.log(value);

});

// => Logs `1` then `2`.

 

_.forEach({ 'a': 1, 'b': 2 }, function(value, key) {

  console.log(key);

});

// => Logs 'a' then 'b' (iteration order is not guaranteed).


_.partial

創建一個函數。 該函數調用 func,並傳入預設的 partials 參數。 這個方法類似 _.bind,除了它不會綁定 this。 


這個 _.partial.placeholder 的值,默認是以 _ 作爲附加部分參數的佔位符。 


注意: 這個方法不會設置 "length" 到函數上。

Ex:

var greet = function(greeting, name) {

  return greeting + ' ' + name;

};

 

var sayHelloTo = _.partial(greet, 'hello');

sayHelloTo('fred');

// => 'hello fred'

 

// 使用了佔位符。

var greetFred = _.partial(greet, _, 'fred');

greetFred('hi');

// => 'hi fred'

_.functions

創建一個函數屬性名稱的數組,函數屬性名稱來自object對象自身可枚舉屬性。

Ex:

function Foo() {

  this.a = _.constant('a');

  this.b = _.constant('b');

}

 

Foo.prototype.c = _.constant('c');

 

_.functions(new Foo);

// => ['a', 'b']


_.bind

創建一個調用func的函數,thisArg綁定func函數中的 this (this的上下文爲thisArg) ,並且func函數會接收partials附加參數。 


_.bind.placeholder值,默認是以 _ 作爲附加部分參數的佔位符。 


注意: 不同於原生的 Function#bind,這個方法不會設置綁定函數的 "length" 屬性。

Ex:

var greet = function(greeting, punctuation) {

  return greeting + ' ' + this.user + punctuation;

};

 

var object = { 'user': 'fred' };

 

var bound = _.bind(greet, object, 'hi');

bound('!');

// => 'hi fred!'

 

// Bound with placeholders.

var bound = _.bind(greet, object, _, '!');

bound('hi');

// => 'hi fred!'


_.isUndefined

檢查 value 是否是 undefined

Ex:

_.isUndefined(void 0);

// => true

 

_.isUndefined(null);

// => false


_.first

獲取數組 array 的第一個元素。

Ex:

_.head([1, 2, 3]);

// => 1

 

_.head([]);

// => undefined


_.reduce

壓縮 collection(集合)爲一個值,通過 iteratee(迭代函數)遍歷 collection(集合)中的每個元素,每次返回的值會作爲下一次迭代使用(愚人碼頭注:作爲iteratee(迭代函數)的第一個參數使用)。 如果沒有提供 accumulator,則 collection(集合)中的第一個元素作爲初始值。(愚人碼頭注:accumulator參數在第一次迭代的時候作爲iteratee(迭代函數)第一個參數使用。)

Ex:

_.reduce([1, 2], function(sum, n) {

  return sum + n;

}, 0);

// => 3

 

_.reduce({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) {

  (result[value] || (result[value] = [])).push(key);

  return result;

}, {});

// => { '1': ['a', 'c'], '2': ['b'] } (無法保證遍歷的順序)


_.isNaN

檢查 value 是否是 NaN。 


注意: 這個方法基於Number.isNaN,和全局的 isNaN 不同之處在於,全局的 isNaN對 於 undefined 和其他非數字的值返回 true。

Ex:

_.isNaN(NaN);

// => true

 

_.isNaN(new Number(NaN));

// => true

 

isNaN(undefined);

// => true

 

_.isNaN(undefined);

// => false


_.isFinite

檢查 value 是否是原始有限數值。 

Ex:

_.isFinite(3);

// => true

 

_.isFinite(Number.MIN_VALUE);

// => true

 

_.isFinite(Infinity);

// => false

 

_.isFinite('3');

// => false


_.object

此方法返回由屬性名稱和值數組組成的對象。提供一個單一的二維數組,例如[[key1, value1], [key2, value2]]或兩個數組,一個屬性名稱和一個相應的值。

Ex:

_.object([['fred', 30], ['barney', 40]]);

// => { 'fred': 30, 'barney': 40 }


_.object(['fred', 'barney'], [30, 40]);

// => { 'fred': 30, 'barney': 40 }


_.every

通過 predicate(斷言函數) 檢查 collection(集合)中的 所有 元素是否都返回真值。一旦 predicate(斷言函數) 返回假值,迭代就馬上停止。predicate(斷言函數)調用三個參數: (value, index|key, collection)。 

Ex:

_.every([true, 1, null, 'yes'], Boolean);

// => false

 

var users = [

  { 'user': 'barney', 'age': 36, 'active': false },

  { 'user': 'fred',   'age': 40, 'active': false }

];

 

// The `_.matches` iteratee shorthand.

_.every(users, { 'user': 'barney', 'active': false });

// => false

 

// The `_.matchesProperty` iteratee shorthand.

_.every(users, ['active', false]);

// => true

 

// The `_.property` iteratee shorthand.

_.every(users, 'active');

// => false

(感覺就像是&&和||操作符一樣,該函數和_.some()的關係→_→)

_.keys

創建一個 object 的自身可枚舉屬性名爲數組。 

Ex:

function Foo() {

  this.a = 1;

  this.b = 2;

}

 

Foo.prototype.c = 3;

 

_.keys(new Foo);

// => ['a', 'b'] (iteration order is not guaranteed)

 

_.keys('hi');

// => ['0', '1']


_.map

創建一個數組, value(值) 是 iteratee(迭代函數)遍歷 collection(集合)中的每個元素後返回的結果。 

Ex:

function square(n) {

  return n * n;

}

 

_.map([4, 8], square);

// => [16, 64]

 

_.map({ 'a': 4, 'b': 8 }, square);

// => [16, 64] (iteration order is not guaranteed)

 

var users = [

  { 'user': 'barney' },

  { 'user': 'fred' }

];

 

// The `_.property` iteratee shorthand.

_.map(users, 'user');

// => ['barney', 'fred']


_.without

創建一個剔除所有給定值的新數組,剔除值的時候,使用SameValueZero做相等比較。 


注意: 不像 _.pull, 這個方法會返回一個新數組。

Ex:

_.without([2, 1, 2, 3], 1, 2);

// => [3]


_.pick

創建一個從 object 中選中的屬性的對象。

Ex:

var object = { 'a': 1, 'b': '2', 'c': 3 };

 

_.pick(object, ['a', 'c']);

// => { 'a': 1, 'c': 3 }


_.identity

這個方法返回首個提供的參數。

Ex:

var object = { 'a': 1 };

 

console.log(_.identity(object) === object);

// => true


_.flatten

減少一級array嵌套深度。

Ex:

_.flatten([1, [2, [3, [4]], 5]]);

// => [1, 2, [3, [4]], 5]

_.chain

創建一個lodash包裝實例,包裝value以啓用顯式鏈模式。要解除鏈必須使用 _#value 方法。

Ex:

var users = [

  { 'user': 'barney',  'age': 36 },

  { 'user': 'fred',    'age': 40 },

  { 'user': 'pebbles', 'age': 1 }

];

 

var youngest = _

  .chain(users)

  .sortBy('age')

  .map(function(o) {

    return o.user + ' is ' + o.age;

  })

  .head()

  .value();

// => 'pebbles is 1'


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章