Array array method of lodash commonly used api

  • _.flattenDeep(array): recurse array into a one-dimensional array

    If you don't use lodash, the personal idea is to use JSON.stringify to convert the array into a string, and then use the replace method of the string to remove the characters '['and the sum. Using this method will be much more convenient']'

_.flattenDeep([1, [2, [3, [4]], 5]]);
// => [1, 2, 3, 4, 5]
  • _.indexOf(array, value, [fromIndex=0]): returns the index value of the first value found in the array array**

    It is often used to detect whether there is a certain value in the array. If there is no such value, it will return a negative value. Example

_.indexOf([3,3], 1);//-1

Specific parameters:
array (Array): The array to be searched.
value (*): The value to look for.
[fromIndex=0] (number): The position to start the query, the default starts from 0

  • _.difference(array, [values]) : filter an array according to another array

    If the array contains the values ​​in [values], then the array removes these values

    example

_.difference([1,2,3,4],[8,4]);   //[1, 2, 3]
  • _.without(array, [values]): Creates a new array excluding all given values
_.without([2, 1, 2, 3], 1, 2);
// => [3]
  • _.drop(array, [n=1]): remove all values ​​before the nth value in the array
_.drop([8,5,4,2,3],2) //[4, 2, 3]
  • _.fill(array, value, [start=0], [end=array.length]): Use the new value to replace the value from the start position to the end position in the array
_.fill([1,2,3,4],'$',1,2) //[1, "$", 3, 4]
  • .findIndex(array, [predicate= .identity], [fromIndex=0]): If the value of the array is a key-value pair, this method can be used to return the index value of the first element that is judged to be a true value by predicate (index )
var users = [
  { 'user': 'barney',  'active': false },
  { 'user': 'fred',    'active': false },
  { 'user': 'pebbles', 'active': true }
];

_.findIndex(users, function(o) { return o.user == 'barney'; });
// => 0
_.findIndex(users, ['active', false]);
// => 0
  • _.head(array) : Get the first value of the array
  • _.pull(array, [values]) : delete the incoming values ​​from the array
var array = [1, 2, 3, 1, 2, 3];

_.pull(array, 2, 3);
console.log(array);
// => [1, 1]

There is also a method _.pullAll(array, values), which differs from the above in that this method accepts an array of values ​​to remove.

  • .remove(array, [predicate= .identity]) : remove the value in the array according to the judgment condition
var array = [1, 2, 3, 4];
var evens = _.remove(array, function(n) {
  return n % 2 == 0;
});

console.log(array);
// => [1, 3]

console.log(evens);
// => [2, 4]
  • _.uniq(array): Array deduplication
  • _.tail(array) : get all values ​​except the first one
  • _.union([arrays]): Union of arrays
_.union([2], [1, 2]);
// => [2, 1]
  • _.unzip(array): Array decomposition
var zipped = _.zip(['fred', 'barney'], [30, 40], [true, false]);
// => [['fred', 30, true], ['barney', 40, false]]

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325854381&siteId=291194637
Recommended