Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Lodash

I would simply like to use the functions from Lodash in my website.

I included in the <head> section:

<script scr="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

Not sure what else I should do? I tried to add:

<script>
var _ = require('lodash'); 
</script>

But I get the following error: Uncaught ReferenceError: require is not defined

like image 377
mat Avatar asked Jun 05 '18 14:06

mat


People also ask

What is Lodash good for?

Lodash helps programmers write more concise and easier to maintain JavaScript code. Lodash contains tools to simplify programming with strings, numbers, arrays, functions and objects. By convention, Lodash module is mapped to the underscore character.

Can we use Lodash in JavaScript?

Lodash is a JavaScript library that works on the top of underscore. js. It helps in working with arrays, strings, objects, numbers, etc.

How do you use Lodash in react?

Using Lodash in your React application is a simple and straightforward process. To start using Lodash in React, you need to: Install Lodash by running npm install lodash. Import Lodash to your project by writing import _ from lodash.


1 Answers

If you load in Lodash using a script tag, the script will automatically expose the _ function, you can directly use it after including it in the document:

document.querySelector("body").onload = function() {console.log(_.sample([1, 2, 3, 4]))}
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
like image 52
Luca Kiebel Avatar answered Oct 21 '22 12:10

Luca Kiebel