Lodash学习笔记

2022-07-28,

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.

Useful Functions

  • Array
    • _.compact(array)
    • _.differenceWith(array, [values], [comparator])
    • _.flattenDeep(array)
    • _.without(array, [values])
    • _.zip([arrays])
  • Object
    • _.omit(object, [props])
    • _.omitBy(object,[predicate= \_.identity])
  • Util
    • _.attempt(func, [args])
    • _.uniqueId([prefix=''])

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 ! !

本文地址:https://blog.csdn.net/qq_40044912/article/details/109379997

《Lodash学习笔记.doc》

下载本文的Word格式文档,以方便收藏与打印。