- 58 Views
- 03/08/2024
Array handling functions in JavaScript
JavaScript provides a wide variety of built-in functions to handle arrays. Below are some common array-handling functions along with examples for each.
1. push()
Adds one or more elements to the end of an array and returns the new length of the array.
fruits.push("orange"); // ["apple", "banana", "orange"]
2. pop()
Removes the last element from an array and returns that element.
let lastFruit = fruits.pop(); // lastFruit = "orange", fruits = ["apple", "banana"]
3. shift()
Removes the first element from an array and returns that element.
let firstFruit = fruits.shift(); // firstFruit = "apple", fruits = ["banana", "orange"]
4. unshift()
Adds one or more elements to the beginning of an array and returns the new length of the array.
fruits.unshift("apple"); // ["apple", "banana", "orange"]
5. concat()
Merges two or more arrays into a new array.
let vegetables = ["carrot", "lettuce"];
let food = fruits.concat(vegetables); // ["apple", "banana", "carrot", "lettuce"]
6. slice()
Returns a shallow copy of a portion of an array into a new array. It does not modify the original array.
let citrus = fruits.slice(1, 3); // citrus = ["banana", "orange"]
7. splice()
Changes the content of an array by removing or replacing existing elements and/or adding new elements.
fruits.splice(2, 1, "pear"); // ["apple", "banana", "pear", "grape"]
8. map()
Creates a new array with the results of calling a provided function on every element in the calling array.
let doubled = numbers.map(num => num * 2); // [2, 4, 6, 8]
9. filter()
Creates a new array with all elements that pass the test implemented by the provided function.
let evenNumbers = numbers.filter(num => num % 2 === 0); // [2, 4]
10. reduce()
Applies a function against an accumulator and each element in the array to reduce it to a single value.
let sum = numbers.reduce((acc, curr) => acc + curr, 0); // sum = 10
11. find()
Returns the value of the first element in the array that satisfies the provided testing function.
let found = numbers.find(num => num > 10); // found = 12
12. findIndex()
Returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1.
let index = numbers.findIndex(num => num > 10); // index = 1
13. sort()
Sorts the elements of an array in place and returns the array.
fruits.sort(); // ["apple", "banana", "grape"]
let numbers = [40, 100, 1, 5, 25, 10];
numbers.sort((a, b) => a - b); // [1, 5, 10, 25, 40, 100]
14. reverse()
Reverses the elements of an array in place.
fruits.reverse(); // ["grape", "apple", "banana"]
15. includes()
Determines whether an array contains a certain element.
let hasApple = fruits.includes("apple"); // true
16. every()
Tests whether all elements in the array pass the test implemented by the provided function.
let allPositive = numbers.every(num => num > 0); // true
17. some()
Tests whether at least one element in the array passes the test implemented by the provided function.
let hasNegative = numbers.some(num => num < 0); // true
18. forEach()
Executes a provided function once for each array element.
numbers.forEach(num => console.log(num)); // Logs 1, 2, and 3
19. flat()
Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
let flatArr = arr.flat(2); // [1, 2, 3, [4]]
20. flatMap()
Maps each element using a mapping function, then flattens the result into a new array.
let flatMappedArr = arr.flatMap(x => [x, x * 2]); // [1, 2, 2, 4, 3, 6]
These are just some of the powerful functions that JavaScript provides for working with arrays. They enable a wide range of functionality, from adding and removing elements to transforming and querying arrays.
Thanks for visit my website