Front-end study notes (18)-Lodash common methods

1.Array
1.1 _.chunk

         _.chunk(array, [size=1])

1.1.1 Concept

        Split an array into blocks of size length and form a new array from these blocks. If the array cannot be split into all chunks of equal length, then the last remaining elements will form a chunk.

1.1.2 Parameters

(1) array (Array) : the array to be processed

(2) [size=1] (number) : The length of each array block

1.1.3 Return

        (Array) : Returns a new array containing split blocks (Note: equivalent to a two-dimensional array).

1.1.4 Example

(1) Input:

_.chunk(['a', 'b', 'c', 'd'], 3);

Output: [[ 'a', 'b', 'c' ] , [ 'd' ]]

(2) Input:

_.chunk([‘1’,’2’,’3’,’4’,’5’,’6’,’7’], 3);

Output: [[ '1', '2', '3' ] , [ '4', '5', '6' ] , [ '7' ]]

1.2 _.findIndex

_.findIndex(array, [predicate=_.identity], [fromIndex=0])

1.2.1 Concept

This method returns the index of the first element whose predicate is true, rather than the element itself.

1.2.2 Parameters

(1) array (Array) : the array to search.

(2) [predicate=_.identity] (Array|Function|Object|string) : This function will be called in each iteration.

(3)[fromIndex=0] (number): The index to search from.

1.2.3 Return

(number) : Returns the index value (index) of the found element, otherwise returns -1.

1.2.4 Example

(1) Input:   

let list3 = [{name:'test1',id:'1'},{name:'test2',id:'2'},{name:'test3',id:'3'},{name:'test4',id:'4'}]

_.findIndex(list3, function(o) { return o.id == '1'; });

Output: 0

1.3 _.fromPairs

        _.fromPairs(pairs)

1.3.1 Concept

        This method returns an object consisting of key-value pairs.

1.3.2 Parameters

(1)pairs (Array) : Key-value pairs.

1.3.3 Return

(Object) : Returns a new object.

1.3.4 Example

(1) Input:   

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

Output:   { 'fred': 30, 'barney': 40 }

1.4 _.join

_.join(array, [separator=','])

1.4.1 Concept

Converts all elements in array to strings separated by separator.

1.4.2 Parameters

(1) array (Array) : Array to be converted.

(2) [separator=','] (string) : Separate elements.

1.4.3 Return

(string) : Returns the connection string.

1.4.4 Example

(1) Input:

 _.join(['a', 'b', 'c'], '~');

Output: 'a~b~c'

1.5 _.slice

        _.slice(array, [start=0], [end=array.length])

1.5.1 Concept

        Crop the array from the start position to the end position, but not including the position of end itself ([,)). Similar to the slice method of arrays , without destroying the array

1.5.2 Parameters

(1) array (Array) : Array to be clipped.

(2)[start=0] (number) : starting position.

(3)[end=array.length] (number) : end position

1.5.3 Return

(Array) : Returns a new array of the cropped part of array array.

1.5.4 Example

(1) Input:

_.slice(['1','2','3','4','5','6','7'],1,-2)

Output: [  '2', '3', '4', '5' ]

1.6 _.zip

        _.zip([arrays])

1.6.1 Concept

        Creates an array of grouped elements, where the first element of the array contains all the first elements of the given array, the second element of the array contains all the second elements of the given array, and so on.

1.6.2 Parameters

(1)[arrays] (...Array) : array to be processed

1.6.3 Return

(Array) : Returns a new array of grouped elements. .

1.6.4 Example

(1) Input:

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

Output: [[ 'fred', 30, true ] , [ 'barney', 40, false ]]

1.7 _.zipObject

        _.zipObject([props=[]], [values=[]])

1.7.1 Concept

        This method is similar to _.fromPairs, except that it accepts 2 arrays, the value in the first array as the attribute identifier (attribute name), and the value in the second array as the corresponding attribute value.

1.7.2 Parameters

(1)[props=[]] (Array): The property identifiers.

(2)[values=[]] (Array): The property values.

1.7.3 Return

(Object): Returns the new object.

1.7.4 Example

(1) Input:

_.zipObject(['a', 'b'], [1, 2]);

Output: { 'a': 1, 'b': 2 }

2. Collection
2.1 _.reduce

        _.reduce(collection, [iteratee=_.identity], [accumulator])

2.1.1 Concept

        Compress the collection (collection) into a value, and traverse each element in the collection (collection) through iteratee (iteration function). The value returned each time will be used as the next iteration (note: as the first element of iteratee (iteration function) parameters used). If no accumulator is provided, the first element in the collection is used as the initial value. (Note: The accumulator parameter is used as the first parameter of iteratee (iteration function) in the first iteration.) iteratee calls 4 parameters: (accumulator, value, index|key, collection) .

2.1.2 Parameters

(1) collection (Array|Object) : Collection used for iteration.

(2) [iteratee=_.identity] (Function) : The function called in each iteration.

(3) [accumulator] (*) : initial value (such as {}, [])

2.1.3 Return

(*) : Returns the accumulated value.

2.1.4 Example

(1) Input:

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

  return sum + n;

}, 0);

Output: 3

(unfinished)

Guess you like

Origin blog.csdn.net/JiangZhengyang7/article/details/132431310