Getting Started with Underscore.js
To get started with Underscore.js, simply include the library in your web page using a script tag:<script src=\"https://cdn.jsdelivr.net/underscorejs/1.8.3/underscore-min.js\"></script>
_
object. For example, the following code uses the _.map()
function to create a new array based on an existing array:
var numbers = [1, 2, 3, 4, 5];
var doubledNumbers = _.map(numbers, function(num) {
return num * 2;
});
console.log(doubledNumbers); // [2, 4, 6, 8, 10]
Working with Arrays in Underscore.js
Underscore.js provides a wide range of functions for working with arrays, including functions for iterating over arrays, filtering and sorting arrays, and manipulating arrays. Here are a few examples:Iterating Over Arrays
The_.each()
function can be used to iterate over an array and perform a function on each element of the array:
var numbers = [1, 2, 3, 4, 5];
_.each(numbers, function(num) {
console.log(num);
}); // 1 2 3 4 5
Filtering Arrays
The_.filter()
function can be used to create a new array that contains only elements that meet a certain condition:
var numbers = [1, 2, 3, 4, 5];
var evenNumbers = _.filter(numbers, function(num) {
return num % 2 === 0;
});
console.log(evenNumbers); // [2, 4]
Sorting Arrays
The_.sortBy()
function can be used to sort an array based on a certain property or function:
var people = [
{name: 'Alice', age: 25},
{name: 'Bob', age: 30},
{name: 'Charlie', age: 20}
];
var sortedPeople = _.sortBy(people, function(person) {
return person.age;
});
console.log(sortedPeople);
// [{name: 'Charlie', age: 20}, {name: 'Alice', age: 25}, {name: 'Bob', age: 30}]
Working with Objects in Underscore.js
Underscore.js provides a wide range of functions for working with objects, including functions for iterating over objects, copying and extending objects, and manipulating object properties. Here are a few examples:Iterating Over Objects
The_.each()
function can also be used to iterate over object properties and perform a function on each property:
var person = {
name: 'Alice',
age: 25,
gender: 'female'
};
_.each(person, function(value, key) {
console.log(key + ': ' + value);
});
// name: Alice
// age: 25
// gender: female
Copying and Extending Objects
The_.clone()
function can be used to create a new copy of an object, while the _.extend()
function can be used to merge two or more objects together:
var person = {
name: 'Alice',
age: 25
};
var copy = _.clone(person);
copy.age = 30;
console.log(person.age); // 25
console.log(copy.age); // 30
var defaults = {
name: 'Unknown',
age: 0
};
var options = {
age: 25
};
var merged = _.extend(defaults, options);
console.log(merged); // {name: 'Unknown', age: 25}
Working with Strings in Underscore.js
Underscore.js provides a range of utility functions for working with strings, including functions for transforming strings, splitting and joining strings, and escaping special characters. Here are a few examples:Transforming Strings
The_.capitalize()
function can be used to capitalize the first letter of a string:
var name = 'alice';
var capitalized = _.capitalize(name);
console.log(capitalized); // 'Alice'
Splitting and Joining Strings
The_.words()
function can be used to split a string into an array of words:
var sentence = 'The quick brown fox';
var words = _.words(sentence);
console.log(words); // ['The', 'quick', 'brown', 'fox']
_.join()
function can be used to join an array of strings into a single string:
var words = ['The', 'quick', 'brown', 'fox'];
var sentence = _.join(words, ' ');
console.log(sentence); // 'The quick brown fox'
Escaping Special Characters
The_.escape()
function can be used to escape special characters in a string, such as HTML tags:
var message = 'Click <a href=\"#\">here</a> to learn more';
var escaped = _.escape(message);
console.log(escaped);
// 'Click <a href="#">here</a> to learn more'