Underscore.js is a JavaScript utility library that was created by Jeremy Ashkenas in 2009. Despite being over a decade
old, this library is still widely utilized by developers for object and array handling as well as a collection of many
other functions. You can easily install underscore using NPM with the command npm install underscore.
The first Underscore function I researched was the _.map function. Map can be used a few different ways; You can supply
parameters of an array and a single attribute to map, or you can enter the array and a function to map multiple
attributes. For these examples, we will be using an array of employees. In the first use of the function, we are
providing the array and a string identifying the ‘name’ property to return an array of all employee names. In the second
example, instead of a single value to search for, we are mapping the selected attributes to be returned from the
function. You can search for any number of attributes within the function and they will be returned as an array of
objects, omitting any data not called for within the mapping function.
const employees = [
{id: 0, name: "Bob", salary: 50000},
{id: 1, name: "Jane", salary: 75000},
{id: 2, name: "Peter", salary: 70000},
{id: 3, name: "Sally", salary: 38000},
{id: 4, name: "Sue", salary: 45000},
]
console.log(_.map(employees, "name"))
console.log(_.map(employees, (employee) => {
return { name: employee.name, salary: employee.salary };
}));
The first Console Log will display an array of the names.
The second Console Log is an array of objects with the name and salary attributes, but does not display the ID attribute as it was note called for in the map function.
Another Underscore function that can be helpful is the _.some function. This function accepts two parameters: an array
to search and a function to assess the array with. If any of the entries in the array fulfill the parameter function, a
Boolean True is returns. Conversely, if none of the entries match the parameter function, False is returned.
In this example, the Some function accepts the array of grades and then checks the grade value of each student to
determine whether or not they have a grade less than 70. Since Mike has been skipping class, his grade is currently
less
than acceptable. Since at least one of the grade values matches the inequality function passed in as a parameter,
the
some function will return True.
const grades = [
{name: "Joe", grade: 88},
{name: "Helen", grade: 97},
{name: "Mike", grade: 66},
{name: "Patricia", grade: 85},
{name: "Luke", grade: 75},
]
let failing = _.some(grades, student => student.grade < 70 );
console.log(failing);
//Expected output: True
We can test the accuracy of this function by changing Mike’s grade. Mike turned in an extra credit
assignment that boosted his grade by five percent, raising it above the cut-line of 70. If we adjust the value
of Mike’s grade and then rerun the Some function, we’ll find that there are no longer any failing students since False
is returned.
grades[2].grade += 5;
failing = _.some(grades, student => student.grade < 70);
console.log(failing);
//expected output: False!
Because Underscore is one of the older NPM libraries, I found a good deal of deprecated functions that were no longer
supported. Be sure to test these functions before implementing them large-scale to ensure that they are still working. I
can assure you that as of today both of these functions are still supported as well as many more!