how to use the values function from the underscore library in javascript

The values function in the Underscore.js library can be used to extract the values from an object and return them in an array. Here is an example usage of values in javascript:

index.tsx
// First, include the underscore library in your project
const _ = require('underscore');

// Define an example object
const obj = {a: 1, b: 2, c: 3};

// Use the `values` function to extract the values from the object
const arr = _.values(obj);

console.log(arr); // Output: [1, 2, 3]
286 chars
11 lines

In the above code, we first include the Underscore library using the require function. We then define an example object obj, which contains key-value pairs. Finally, we use the values function to extract the values from obj and store them in an array called arr. The output of this code is an array containing the values [1, 2, 3].

Note that the values function works with both arrays and objects. When used with an array, it simply returns a copy of the array.

gistlibby LogSnag