1. Array.prototype.reduce()
The reduce
method executes a reducer function on each element of the array, resulting in a single output value.
Example: Summing an array of numbers
jaconst numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // Output: 15
2. Array.prototype.flat()
The flat
method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
Example: Flattening a nested array
javascripconst nestedArray = [1, [2, [3, 4]], 5];
const flattened = nestedArray.flat(2);
console.log(flattened); // Output: [1, 2, 3, 4, 5]
3. Promise.all()
The Promise.all()
method takes an iterable of Promise objects and, when all of the promises have resolved, returns a single Promise that resolves with an array of the results.
Example: Fetching multiple resources
javascripconst fetchData1 = () => Promise.resolve('Data 1');
const fetchData2 = () => Promise.resolve('Data 2');
Promise.all([fetchData1(), fetchData2()])
.then(results => console.log(results)) // Output: ['Data 1', 'Data 2']
.catch(error => console.error(error));
4. Object.entries()
The Object.entries()
method returns an array of a given object's own enumerable string-keyed property [key, value] pairs.
Example: Converting an object to an array of entries
javascriptconst obj = { a: 1, b: 2, c: 3 };
const entries = Object.entries(obj);
console.log(entries); // Output: [['a', 1], ['b', 2], ['c', 3]]
5. String.prototype.includes()
The includes
method determines whether one string can be found within another string, returning true or false as appropriate.
Example: Checking for a substring
javascrconst str = 'Hello, world!';
const hasHello = str.includes('Hello');
console.log(hasHello); // Output: true
6. Set()
A Set
is a collection of values where each value must be unique. It can be used to remove duplicates from an array.
Example: Removing duplicates
javascriptconst numbers = [1, 2, 2, 3, 4, 4, 5];
const uniqueNumbers = [...new Set(numbers)];
console.log(uniqueNumbers); // Output: [1, 2, 3, 4, 5]
7. Function.prototype.bind()
The bind
method creates a new function that, when called, has its this
keyword set to the provided value.
Example: Setting context for a method
javascriptconst obj = { value: 42 };
function getValue() {
return this.value;
}
const boundGetValue = getValue.bind(obj);
console.log(boundGetValue()); // Output: 42
8. Array.prototype.find()
The find
method returns the value of the first element in the array that satisfies the provided testing function.
Example: Finding an object in an array
javasllconst users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
];
const user = users.find(u => u.id === 2);
console.log(user); // Output: { id: 2, name: 'Bob' }
9. Array.prototype.sort()
The sort
method sorts the elements of an array in place and returns the sorted array. It can accept a comparison function for custom sorting.
Example: Sorting an array of numbers
javasconst arr = [4, 2, 5, 1, 3];
arr.sort((a, b) => a - b);
console.log(arr); // Output: [1, 2, 3, 4, 5]
10. Array.prototype.some()
The some
method tests whether at least one element in the array passes the test implemented by the provided function.
Example: Checking for at least one eve number
javasconst numbers = [1, 3, 5, 7, 8];
const hasEven = numbers.some(num => num % 2 === 0);
console.log(hasEven); // Output: true
These methods can help you write more efficient and readable JavaScript code. If you want more examples or explanations on specific methods, feel free to ask!
No comments:
Post a Comment