Very easy to use tool for JavaScript library --lodash

First, what is lodash?

JavaScript lodash tool library is a library with a consistent interface, modular, high-performance characteristics.
lodash is a javascript library module is also commonly used Node JS, it inside the package handler of many strings, arrays, and other common data type of the object, wherein the part is yet to be defined ECMAScript specifications, but was recognized by the industry auxiliary functions.

Two, lodash related documents

API documentation:

Three, lodash modules

  • The Array, suitable for array types, such as padding data, find elements, an array slice and other operations;
  • Collection, suitable for the type of array and object, section applies to string, such as a packet, to find, such as filtration;
  • Function, applicable to the function type, such as a throttle, delay, buffer, and other operations provided hooks;
  • Lang, generally applicable to all types, the type commonly used in the determination and execution type conversion;
  • The Math, for numeric type, commonly used in performing mathematical operations;
  • Number, adapted to generate a random number, comparing the relationship between the numerical value range;
  • Object, applicable to the type of the object, the object used to create, expand, type conversion, retrieval, collection and other operations;
  • Seq, commonly used in the call to create a chain to improve execution performance (lazy evaluation);
  • String, String suitable type;

Fourth, the installation and use

To _.groupBy () method, for example in terms of:

(A) using the method
  1. Installation command:npm i --save lodash
  2. Instructions:
import _ from 'lodash';
let names = require('./names'); names = _.groupBy(require('./names'), (name) => name[0].toUpperCase()); 
(Ii) Parameter Detail

V. For "chestnut" child

We want to achieve a list of cities packets, similar to the address book list micro letter, Zhang map:

Suppose we now only such data:

那怎么实现呢?
用groupBy就可以实现分组啦~

...
import _ from 'lodash'; let cities = require('./beforeCity.json'); ... getCityInfo(){ console.log('cities=',cities); let cityList = []; cityList = _.groupBy(cities, (city) => city.pinyin[0]); console.log('cityList=',cityList); } ... 

分组结果如下:

 



Guess you like

Origin www.cnblogs.com/it-Ren/p/11819419.html