?? Mastering JavaScript Arrays: Unleash the Power with ?? Exciting Methods! ????
Mehar Usman
Front-end Web Developer| Vue.js | Nuxt.js | React.js | Next.js| @EFU Life Assurance LTD
Certainly! Here's an explanation of several common array methods in JavaScript, presented in an easy-to-understand way with examples:
1. push()
Explanation: Adds elements to the end of an array.
Example:
let fruits = ["apple", "orange"]; fruits.push("banana", "grape"); // Result: ["apple", "orange", "banana", "grape"]
2. pop()
Explanation: Removes the last element from an array.
Example:
let fruits = ["apple", "orange", "banana"]; let removedFruit = fruits.pop(); // Result: removedFruit = "banana", fruits = ["apple", "orange"]
3. unshift()
Explanation: Adds elements to the beginning of an array.
Example:
let fruits = ["banana", "grape"]; fruits.unshift("orange", "apple"); // Result: ["orange", "apple", "banana", "grape"]
4. shift()
Explanation: Removes the first element from an array.
Example:
let fruits = ["orange", "apple", "banana"]; let removedFruit = fruits.shift(); // Result: removedFruit = "orange", fruits = ["apple", "banana"]
5. concat()
Explanation: Combines two arrays into a new array.
Example:
let fruits = ["apple", "orange"]; let vegetables = ["carrot", "broccoli"]; let combinedArray = fruits.concat(vegetables); // Result: ["apple", "orange", "carrot", "broccoli"]
6. slice()
Explanation: Creates a new array from a portion of an existing array.
Example:
let fruits = ["apple", "orange", "banana", "grape"]; let slicedArray = fruits.slice(1, 3); // Result: slicedArray = ["orange", "banana"], fruits = ["apple", "orange", "banana", "grape"]
7. splice()
Explanation: Changes the contents of an array by removing or replacing existing elements.
Example:
领英推荐
let fruits = ["apple", "orange", "banana", "grape"]; fruits.splice(1, 2, "kiwi", "melon"); // Result: ["apple", "kiwi", "melon", "grape"]
8. indexOf()
Explanation: Returns the index of the first occurrence of a specified element in an array.
Example:
let fruits = ["apple", "orange", "banana", "grape"]; let index = fruits.indexOf("banana"); // Result: index = 2
9. includes()
Explanation: Checks if an array includes a specific element.
Example:
let fruits = ["apple", "orange", "banana", "grape"]; let hasBanana = fruits.includes("banana"); // Result: hasBanana = true
Example:
let fruits = ["apple", "orange", "banana", "grape"]; let fruitString = fruits.join(", "); // Result: fruitString = "apple, orange, banana, grape"
11. reverse()
Explanation: Reverses the order of elements in an array.
Example:
let fruits = ["apple", "orange", "banana", "grape"]; fruits.reverse(); // Result: ["grape", "banana", "orange", "apple"]
12. filter()
Explanation: Creates a new array with elements that pass a test specified by a function.
Example:
let numbers = [1, 5, 8, 10, 3]; let filteredArray = numbers.filter(num => num > 5); // Result: filteredArray = [8, 10]
13. map()
Explanation: Creates a new array with the results of calling a provided function on every element.
Example:
let numbers = [1, 2, 3, 4]; let squaredNumbers = numbers.map(num => num * num); // Result: squaredNumbers = [1, 4, 9, 16]
14. reduce()
Explanation: Applies a function against an accumulator and each element in the array to reduce it to a single value.
Example:
let numbers = [1, 2, 3, 4]; let sum = numbers.reduce((acc, num) => acc + num, 0); // Result: sum = 10
These examples demonstrate the basic usage and purpose of each array method. Understanding these methods will give you powerful tools for working with arrays in JavaScript.
?? Follow Mehar Usman for more Web Development, Programming Tips & Tricks, and Job Opportunities.
?? ?????? ????????, if you found it helpful !
?? ???????????? it to your network !
?? ???????? it for the future !
?? ?????????? it with your connections !
?? ???????????????your?thoughts?!