Source Code

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.

let fruits = ["apple", "banana"];
fruits.push("orange");  // ["apple", "banana", "orange"]

2. pop()
Removes the last element from an array and returns that element.

let fruits = ["apple", "banana", "orange"];
let lastFruit = fruits.pop();  // lastFruit = "orange", fruits = ["apple", "banana"]

3. shift()
Removes the first element from an array and returns that element.

let fruits = ["apple", "banana", "orange"];
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.

let fruits = ["banana", "orange"];
fruits.unshift("apple");  // ["apple", "banana", "orange"]

5. concat()
Merges two or more arrays into a new array.

let fruits = ["apple", "banana"];
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 fruits = ["apple", "banana", "orange", "grape"];
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.

let fruits = ["apple", "banana", "orange", "grape"];
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 numbers = [1, 2, 3, 4];
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 numbers = [1, 2, 3, 4, 5];
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 numbers = [1, 2, 3, 4];
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 numbers = [5, 12, 8, 130, 44];
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 numbers = [5, 12, 8, 130, 44];
let index = numbers.findIndex(num => num > 10);  // index = 1

13. sort()
Sorts the elements of an array in place and returns the array.

let fruits = ["banana", "apple", "grape"];
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.

let fruits = ["banana", "apple", "grape"];
fruits.reverse();  // ["grape", "apple", "banana"]

15. includes()
Determines whether an array contains a certain element.

let fruits = ["apple", "banana", "orange"];
let hasApple = fruits.includes("apple");  // true

16. every()
Tests whether all elements in the array pass the test implemented by the provided function.

let numbers = [1, 2, 3, 4, 5];
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 numbers = [1, -2, 3, -4];
let hasNegative = numbers.some(num => num < 0);  // true

18. forEach()
Executes a provided function once for each array element.

let numbers = [1, 2, 3];
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 arr = [1, [2, [3, [4]]]];
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 arr = [1, 2, 3];
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