Chunk array partition

We all know that chunk is actually a partition, so the chunk method will partition the array

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

Divide an array into blocks of size and form these blocks into a new array. If the array cannot be split into all equal length blocks, the last remaining elements form a block.

parameter

  1. Array (array) : array to be processed
  2. [size=1] (Number) : indicates the length or size of each chunk. The default value is 1

return

Returns a new array containing split blocks

console.log(_.chunk(['a'.'b'.'c'.'d']));//size The default value is 1
// [ [ 'a' ], [ 'b' ], [ 'c' ], [ 'd' ] ]
console.log(_.chunk(['a'.'b'.'c'.'d'].2));
// [ [ 'a', 'b' ], [ 'c', 'd' ] ]
console.log(_.chunk(['a'.'b'.'c'.'d'].3));
// [ [ 'a', 'b', 'c' ], [ 'd' ] ]
console.log(_.chunk(['a'.'b'.'c'.'d'].5));
// [ [ 'a', 'b', 'c', 'd' ] ]
Copy the code

compact

_.compact(array)
Copy the code

Creates a new array containing all the non-false value elements in the original array. Examples of false, null,0, “”, undefined, and NaN are all considered “false values”.

parameter

  1. Array (array) : array to be processed

return

Returns a new array filtered out of false values

console.log(_.compact([0.1.false.2.' '.null.undefined.NaN.3]))
// => [1, 2, 3]
Copy the code

Concat joins arrays

_.concat(array, [values])
Copy the code

parameter

  1. Array (array) : array to be processed
  2. Values: indicates the connection value

return

Returns the new array after joining

var array = [1];
var other = _.concat(array, 2[3], [[4]]);
 
console.log(other);
// => [1, 2, 3, [4]]
 
console.log(array);
/ / = > [1]
Copy the code

Do not change the original array array

difference

_.difference(array, [values])
Copy the code

Creates an array with unique array values that are not included in any other given array. (Note: create a new array of values that exclude values from the given array for the first number (array argument).) The method uses SameValueZero for equality comparison. The order of the resulting values is determined by the order in the first array.

parameter

  1. Array (array) : array to be processed
  2. Values: indicates the excluded value

return

Returns the filtered new array

console.log(_.difference([3.2.1], [4.2]));/ / (3, 1)
console.log(_.difference([3.2.1].2));/ / [3, 2, 1]
let array=[3.2.1];
console.log(_.difference(array, [2]));/ / (3, 1)
console.log(array);/ / [3, 2, 1]
Copy the code

Unlike concat, values must be an array type. Second, the original array is not changed.

differenceBy

_.differenceBy(array, [values], [iteratee=_.identity])
Copy the code

This method is similar to _. Difference, except that it takes an iteratee that calls each element in array and values to produce the criteria for comparison. The resulting value is selected from the first array. Iteratee calls one argument :(value). (Note: First use an iterator to iterate over each element in array and values, respectively, returning the value as the comparison value).

parameter

  1. Array (array) : array to be processed
  2. Values: indicates the excluded value
  3. [iteratee = _. Identity] (Array | Function | Object | string) : iteratee calls to each element.

return

Returns the filtered new array

console.log(_.differenceBy([3.1.2.2.1.3], [4.4.2.5].Math.floor));
/ / = > [3.1, 1.3]
 
// The `_.property` iteratee shorthand.
console.log(_.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1}].'x'));
// => [{ 'x': 2 }]

console.log(_.differenceBy([{ 'x': 2 }, 'x'], ['x'].'x'));
// => [{ 'x': 2 }]
Copy the code

This method is similar to Map, except that it does not modify array elements, and it accepts strings and so on in addition to functions.

An accept function is a function that compares each array item, and a string is typically an object property