Lodash学习笔记

these days, I am learning English and trying to improve both my language skills and my technology skills . So if you find any errors in my blog, please leave a comment.

Array

_.compact(array)

creat a arrary which include all the non-false elements in the old array, eg.false,null,0,"",undefined and NaN are all regarded as false elements which convert into boolean is false .

try:

_.compact([10,12,false,'',455])
// =>[10,12,455]

use scene:
when we transform a string into an arrary, there may be some false elements, then this function can escape using our extra effect to filter the false elements.

_.differenceWith(array, [values], [comparator])

accept a comparator and use it to compare the elements in array and values[], creat a array as a result including the elements choosen from the first array which compare result was false,

try:

var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
 
_.differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual);
// => [{ 'x': 2, 'y': 1 }]

use scene:
not only can get the different parts of array but also can get the same part of the arrays, if you choose the right comparator.

_.flattenDeep(array)

Recurse array into a one-dimensional array.

try:

_.flattenDeep([1, [2, [3, [4]], 5]]);
// => [1, 2, 3, 4, 5]

use scene:
it is convient for us to format the array, just put the array in a array!

_.without(array, [values])

use the array to create a new array without the values.

try:

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

_.zip([arrays])

create an [array], this first array element includes all the first elememt in the arrays,and the second array element includes all the second elememt in the arrays,and so on.

try:

_.zip(['fred', 'barney'], [30, 40], [true, false]);
// => [['fred', 30, true], ['barney', 40, false]]

there is also some functions about collection, if you want learn more,please check the documents.

Object

_.omit(object, [props])

This method is an object, which consists of the object itself and inherited enumerable properties except for ignoring properties

try:

var object = { 'a': 1, 'b': '2', 'c': 3 };
 
_.omit(object, ['a', 'c']);
// => { 'b': '2' }

_.omitBy(object,[predicate= _.identity])

this method is an object, this object ignores the property that predicate (predicate function) judges is not true value, the object itself and inherited enumerable properties are composed. The predicate is called with 2 parameters: (value, key).

try:

omitBy(form.getFieldsValue(), isEmpty)

use scene:
this is a function that I usually use to delete the empty value from the form value,it is really usefull

Util

_.attempt(func, [args])

try to call func, return the result or catch the error object. Any additional parameters will be passed to func when called.

try:

// Avoid throwing errors for invalid selectors.
var elements = _.attempt(function(selector) {
  return document.querySelectorAll(selector);
}, '>_>');
 
if (_.isError(elements)) {
  elements = [];
}

_.uniqueId([prefix=’’])

generate unique Id ,if you give a prefix ,it will been added at the front of Id

try :

_.uniqueId('contact_');
// => 'contact_104'
 
_.uniqueId();
// => '105'

there are so many useful and powerful utils in the lodash, so if you are interested, you can learn more information on the lodash website

If it helps you, can you give a like ! ! Thank you ! !

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我也秃了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值