Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lodash: only importing chain does not work nice

The following code works:

let _ = require('lodash');
let arr= _([1,2,3,4]);
let b = arr.map((e)=>e^2);

The following does not work:

let chain = require('lodash/chain');
let arr= chain([1,2,3,4]);
let b = arr.map((e)=>e^2);

It gives me the following error:

arr.map is not defined.

Is there a way to use chain without importing the whole global object?

like image 524
David Michael Gang Avatar asked Feb 18 '16 16:02

David Michael Gang


People also ask

What does lodash chain do?

The Lodash _. chain() method used to wrap the value with explicit method chain sequences enabled.

Is lodash heavy?

While lodash offeres a tremendous library of functionality, much of it is no longer required, and the cost of importing lodash into your application can be huge, well over 600kb if your compiler doesn't shake the unrequired code, or you use lodash throughout your application.

Do you need to import lodash?

How to correctly include specific functions of the popular lodash library in your web JavaScript project without having to import the entire library. Lodash is an extremely popular JavaScript library that provides a lot of useful functions for working with strings, arrays and objects in your web projects.


1 Answers

You need at least core:

var _ = require('lodash/core');
let arr= _([1,2,3,4]);
let b = arr.map((e)=>e^2);

You can also roll your own custom lodash package.

like image 116
Benny Bottema Avatar answered Nov 16 '22 05:11

Benny Bottema