成都网站建设设计

将想法与焦点和您一起共享

项目中使用过的lodash方法总结-创新互联

_.findWhere

创新互联公司主要从事做网站、成都网站建设、网页设计、企业做网站、公司建网站等业务。立足成都服务兰州,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792

对集合中的每个元素和源进行深度比较。

该方法支持比较数组、布尔值、数字、日期对象,对象的对象,正则表达式、字符串。

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'

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


网站栏目:项目中使用过的lodash方法总结-创新互联
URL标题:http://chengdu.cdxwcx.cn/article/pdphd.html