wtorek, 13 lutego 2018

Angular 5 and underscore library

Are you miss underscore library in your most recent Angular 5 application?

It is easier than you may think. The only you need to do is add two dependencies to your package.json file.

Install dependecies

You can do this with npm command.

npm install --save @types/underscore
and
npm install --save @underscore

How to use it?

In order to use underscore library in your component or service class you have to import library in the following way:
import * as _ from "underscore";
That is basically all - now you can take all the goodies from underscore. For example, to fetch all unique key values from an array of key-value struct we can do this way:
public uniqueKeys(input: {[key: string]: string}[]): string[] {

   let keys = _
     .chain(variableSetContent)
     .map(a => _.keys(a))
     .flatten()
     .uniq(false)
     .value()

Brak komentarzy:

Prześlij komentarz