Lodash Interview Questions
The `_.random()` function produces random values between the inclusive lower and upper bounds.
Example :
``````<!DOCTYPE html>
<html>
<title>Lodash Tutorial</title>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>
<script type="text/javascript">
// Getting array slice
var r = _.random(15);
console.log(r);

r = _.random(5, 15);
console.log(r);
</script>
<body></body>
</html>``````
Output :
``````8
7``````
Lodash allows computing the maximum and minimum values of an array.

Example :
``````<!DOCTYPE html>
<html>
<title>Lodash Tutorial</title>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>

<script type="text/javascript">
const vals = [-3, 4, 0, 12, 43, 9, -12];

var min = _.min(vals);
console.log(min);

var max = _.max(vals);
console.log(max);

max = _.max(_.range(5, 25));
console.log(max);

const obs = [{n: 12}, {n: -4}, {n: 4}, {n: -11}];

min = _.minBy(obs, 'n');
console.log(min);

max = _.maxBy(obs, 'n');
console.log(max);
</script>
<body></body>
</html>``````
Output :
``````-12
43
24
{ n: -11 }
{ n: 12 }``````

The `_.sum()` function calculates the sum of array values.
Example :
``````const vals = [-2, 0, 3, 7, -5, 1, 2];

const sum = _.sum(vals);
console.log(sum);``````
In the code example, we compute and print the sum of array values.
``````6
``````
The Lodash `_.range()` function creates an array of numbers. The function accepts the start, end, and step parameters.
Example :
``````<!DOCTYPE html>
<html>
<title>Lodash Tutorial</title>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>

<script type="text/javascript">
const vals = _.range(10);
console.log(vals);

const vals2 = _.range(0, 15);
console.log(vals2);

const vals3 = _.range(0, 15, 5);
console.log(vals3);
</script>
<body></body>
</html>``````
Output :
``````[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ]
[ 0, 5, 10 ]``````
The `_.delay()` function delays the execution of a function for the specified amount of milliseconds.
Example :
``````<!DOCTYPE html>
<html>
<title>Lodash Tutorial</title>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>

<script type="text/javascript">
function message()
{
console.log("Some message");
}
_.delay(message, 150);
console.log("Some other message");
</script>
<body></body>
</html>``````

The example outputs two messages. The first one is delayed for 150ms. Here is the output :

Some other message
Some message
The `_.times()` executes the function n times.
Example :
``````<!DOCTYPE html>
<html>
<title>Lodash Tutorial</title>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>

<script type="text/javascript">
_.times(4, () => {
console.log("Learning");
})
</script>
<body></body>
</html>``````

In the example, we execute the inner function four times. The function prints a word to the console.
``````Learning
Learning
Learning
Learning``````
`_.reduce` is a little bit like a filter function. The only difference is that you can choose the form of the returned object.
Example :
``````<!DOCTYPE html>
<html>
<title>Lodash Tutorial</title>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>

<script type="text/javascript">
var users = [
{ name: "John", age: 30 },
{ name: "Jane", age: 28 },
{ name: "Bill", age: 65 },
{ name: "Emily", age: 17 },
{ name: "Jack", age: 30 }
]

var reducedUsers = _.reduce(users, function (result, user) {
if(user.age >= 18 && user.age <= 59) {
(result[user.age] || (result[user.age] = [])).push(user);
}

return result;
}, {});

console.log(JSON.stringify(reducedUsers));
</script>
<body></body>
</html>``````
The above HTML prints below output on the console :
``{"28":[{"name":"Jane","age":28}],"30":[{"name":"John","age":30},{"name":"Jack","age":30}]}``
`_.keyBy` is a very useful method. It helps a lot when trying to get an object with a specific property. Let’s say we have 100 users and we want to get the user with Id 1.
Example :
``````<!DOCTYPE html>
<html>
<title>Lodash Tutorial</title>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>

<script type="text/javascript">
var users = [
{ id: 1, firstName: "John", lastName: "Doe", age: 28, gender: "male" },
{ id : 2, firstName: "Jane", lastName: "Doe", age: 5, gender: "female" },
{ id : 3, firstName: "Jim", lastName: "Carrey", age: 54, gender: "male" },
{ id : 4, firstName: "Kate", lastName: "Winslet", age: 40, gender: "female" }
];

users = _.keyBy(users, "id");

var user = users[1];
console.log(user);
</script>
<body></body>
</html>``````
Output :
``{id: 1, firstName: "John", lastName: "Doe", age: 28, gender: "male"}``
The `_.shuffle()` function shuffles a collection.
Example :
``````<!DOCTYPE html>
<html>
<title>Lodash Tutorial</title>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>

<script type="text/javascript">
words = ['sky', 'wood', 'forest', 'falcon',
'pear', 'ocean', 'universe'];

console.log(_.shuffle(words));
console.log(_.shuffle(words));
console.log(_.shuffle(words));
console.log(words);
</script>
<body></body>
</html>``````
Output :
``````["universe", "pear", "ocean", "sky", "wood", "falcon", "forest"]
["forest", "pear", "ocean", "falcon", "wood", "universe", "sky"]
["ocean", "sky", "wood", "universe", "pear", "forest", "falcon"]
["sky", "wood", "forest", "falcon", "pear", "ocean", "universe"]``````
Locash library contains several functions that work with the case of words :

* `_.camelCase`
* `_.capitalize`
* `_.kebabCase`
* `_.lowerCase`
* `_.upperCase`

``````<!DOCTYPE html>
<html>
<title>Lodash Tutorial</title>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.11/lodash.min.js"></script>

<script type="text/javascript">
const words = ["sky", "Sun", "Blue Island"];

console.log(_.map(words, _.camelCase));
console.log(_.map(words, _.capitalize));
console.log(_.map(words, _.kebabCase));
console.log(_.map(words, _.lowerCase));
console.log(_.map(words, _.upperCase));
</script>
``````[ 'sky', 'sun', 'blueIsland' ]