Underscore.js _.value() Function
Last Updated :
10 Jul, 2020
Underscore.js is a JavaScript library that makes operations on arrays, string, objects much easier and handy.
The _.value() function is used to extract the value of the wrapped object. This function is mainly used with chain function in JavaScript.
Note: It is very necessary to link the underscore CDN before going and using underscore functions in the browser. When linking the underscore.js CDN The “_” is attached to the browser as global variable.
Syntax:
_.chain(obj).value()
Parameters: This function does not accept any parameter. It is chained with different functions. “.value()” is used to extract the value of the object i.e. array, string etc.
Return Value: It returns the value of the object of object type. If the object is an array it returns an array, If the object is a string it returns a string and so on.
Example 1:
<!DOCTYPE html>
< html >
< head >
< script src =
</ script >
</ head >
< body >
< script >
let arr = ["c", "d", "a", "b"];
console.log(`original array is ${arr}`);
// Wrapped object is array so
// its values is returned
let newArr = _.chain(arr).sort().value();
// Changes made in original array.
console.log(`original array is ${arr}`);
console.log(`new array is ${newArr}`);
</ script >
</ body >
</ html >
|
Output:
Example 2:
<!DOCTYPE html>
< html >
< head >
< script src =
</ script >
</ head >
< body >
< script >
let str = "geeks for geeks"
console.log(`original string is ${str}`);
// Wrapped object is str so its
// values is returned
let newstr = _.chain(str).toArray()
.reverse().join("").value();
// Printing the new string
console.log(`new string is ${newstr}`);
</ script >
</ body >
</ html >
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...