Lädt...

🔧 15 Must-Know Javascript array methods


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Arrays are an essential part of any programming language, and JavaScript is no exception. With arrays, developers can store and manipulate collections of data, including strings, numbers, and even objects. In this article, we will cover 15 must-know JavaScript array methods that everyone should know.

push() – Adds one or more elements to the end of an array.

The push() method adds one or more elements to the end of an array and returns the new length of the array. This method is useful when you need to add elements to an array without specifying the index.

const array = [1, 2, 3];  array.push(4, 5);  console.log(array); // Output: [1, 2, 3, 4, 5]

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

The pop() method removes and returns the last element from an array. This method is useful when you need to remove an element from the end of an array.

const array = [1, 2, 3];
    const lastElement = array.pop();
    console.log(array); // Output: [1, 2]
    console.log(lastElement); // Output: 3

shift() – Removes and returns the first element from an array

The shift() method removes and returns the first element from an array. This method is useful when you need to remove an element from the beginning of an array.

const array = [1, 2, 3];
    const firstElement = array.shift();
    console.log(array); // Output: [2, 3]
    console.log(firstElement); // Output: 1

unshift() – Adds one or more elements to the beginning of an array

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array. This method is useful when you need to add elements to an array at the beginning.

const array = [1, 2, 3];
    array.unshift(4, 5);
    console.log(array); // Output: [4, 5, 1, 2, 3]

splice() – Adds or removes elements from an array at a specified index

The splice() method adds and removes elements from an array at a specified index. This method can be used to add or remove elements from anywhere in the array.

const array = [1, 2, 3, 4];
    array.splice(1, 2, 5, 6);
    console.log(array); // Output: [1, 5, 6, 4]

slice() – Returns a copy of a portion of an array specified by its starting and ending indexes

The slice() method returns a copy of a portion of an array specified by its starting and ending indexes. This method can be used to create a new array that contains a subset of the original array.

const array = [1, 2, 3, 4];
    const newArray = array.slice(1, 3);
    console.log(newArray); // Output: [2, 3]

concat() – Combines two or more arrays and returns a new array

The concat() method combines two or more arrays and returns a new array. This method can be used to join arrays together without modifying the original arrays.

const array1 = [1, 2];
    const array2 = [3, 4];
    const newArray = array1.concat(array2);
    console.log(newArray); // Output: [1, 2, 3, 4]

join() – Joins all elements of an array into a string

The join() method joins all elements of an array into a string, using a specified separator. This method can be used to create a string from an array.

const array = [1, 2, 3];  const string = array.join("-");  console.log(string); // Output: "1-2-3"

indexOf() – Returns the index of the first occurrence of a specified element in an array

The indexOf() method returns the index of the first occurrence of a specified element in an array. If the element is not found, this method returns -1.

const array = [1, 2, 3];  const index = array.indexOf(2);  console.log(index); // Output: 1

lastIndexOf() – Returns the index of the last occurrence of a specified element in an array

The lastIndexOf() method returns the index of the last occurrence of a specified element in the array. If the element is not found, this method returns -1.

const array = [1, 2, 3, 2];  const index = array.lastIndexOf(2);  console.log(index); // Output: 3

forEach() – Executes a provided function once for each element in an array

The forEach() method executes a provided function once for each element in an array. This method can be used to perform an operation on each element of an array.

const array = [1, 2, 3];  array.forEach((element) => {  console.log(element);  });  // Output:  // 1  // 2  // 3

map() – Creates a new array with the results of calling a provided function on every element in the array

The map() method creates a new array with the results of calling a provided function on every element in the array. This method can be used to create a new array based on the original array.

const array = [1, 2, 3];  const newArray = array.map((element) => {  return element * 2;  });  console.log(newArray); // Output: [2, 4, 6]

filter() – Creates a new array with all elements that pass a test specified by a provided function

The filter() method creates a new array with all elements that pass a test specified by a provided function. This method can be used to create a new array based on a condition.

const array = [1, 2, 3];  const newArray = array.filter((element) => {  return element > 1;  });  console.log(newArray); // Output: [2, 3]

reduce() – Executes a provided function on each element of the array and returns a single value

The reduce() method executes a provided function on each element of the array and returns a single value. This method can be used to perform an operation on all elements of an array and return a single value.

const array = [1, 2, 3];  const sum = array.reduce((accumulator, currentValue) => {  return accumulator + currentValue;  }, 0);  console.log(sum); // Output: 6

sort() – Sorts the elements of an array in place

The sort() method sorts the elements of an array in place. This method can be used to sort an array in ascending or descending order.

const array = [3, 2, 1];
array.sort();
console.log(array); // Output: [1, 2, 3]

Conclusion
In this article, we have covered 15 must-know JavaScript array methods that everyone should know. These methods are essential for working with arrays in JavaScript and can greatly simplify your code. By using these methods, you can perform a wide range of operations on arrays, such as adding, removing, sorting, and filtering elements. Whether you’re a beginner or an experienced developer, mastering these array methods will make your life easier and your code more efficient.

Those were the 15 must-know javascript array methods for you. I really hoped that you enjoyed reading this article.

Follow me on socials:

Twitter: https://twitter.com/Hy_piyush
Instagram: https://www.instagram.com/piyush_kesarwani22/
Linkedin: https://www.linkedin.com/in/piyush-kesarwani-809a30219/
GitHub: https://github.com/piyushkesarwani

...

🔧 Upcoming JavaScript Features: Simplifying Array Combinations with `Array.zip` and `Array.zipKeyed`


📈 34.77 Punkte
🔧 Programmierung

🔧 JavaScript: Arrays, Array Properties, Array Methods: push, pop, shift, unshift, Stacks, and Queues!


📈 34.49 Punkte
🔧 Programmierung

🔧 JavaScript Array Tutorial – Array Methods in JS


📈 34.49 Punkte
🔧 Programmierung

🔧 Learn the JavaScript Array.every() and Array.some() methods


📈 34.49 Punkte
🔧 Programmierung

🔧 JavaScript Array Methods Examples: A Comprehensive Guide (31 Methods)


📈 34.2 Punkte
🔧 Programmierung

🔧 JavaScript Array Methods, String Methods, & Math.random()


📈 34.2 Punkte
🔧 Programmierung

🔧 Instance methods can call other instance methods, instance variables, class methods, or class variables


📈 28.69 Punkte
🔧 Programmierung

🔧 Select Element in Array() to a new Array() JavaScript


📈 24.92 Punkte
🔧 Programmierung

🔧 JavaScript Array Length – How to Find the Length of an Array in JS


📈 24.92 Punkte
🔧 Programmierung

🔧 JS Sum of an Array – How to Add the Numbers in a JavaScript Array


📈 24.92 Punkte
🔧 Programmierung

🔧 JavaScript Sort Array - How to Sort an Array Accurately


📈 24.92 Punkte
🔧 Programmierung

🐧 Convert Array of Starings to Array of Numbers in JavaScript


📈 24.92 Punkte
🐧 Linux Tipps

🔧 Turbo Array: Supercharge Your JavaScript Array Operations 🚀


📈 24.92 Punkte
🔧 Programmierung

🔧 Find Array Element in the Array using JavaScript


📈 24.92 Punkte
🔧 Programmierung

🔧 Find Array Element in the Array using JavaScript


📈 24.92 Punkte
🔧 Programmierung

🔧 8 Essential JavaScript Array Methods


📈 24.64 Punkte
🔧 Programmierung

🔧 JavaScript Array Methods: Understanding `flatMap`


📈 24.64 Punkte
🔧 Programmierung

🔧 Javascript | Mutating array methods | push, pop, sort, splice


📈 24.64 Punkte
🔧 Programmierung

🔧 Exploring JavaScript Array Methods with Examples


📈 24.64 Punkte
🔧 Programmierung

🔧 Algorithms Behind JavaScript Array Methods


📈 24.64 Punkte
🔧 Programmierung

🔧 10 JavaScript Array Methods That Will Make You Look Like a Pro


📈 24.64 Punkte
🔧 Programmierung

🔧 JavaScript New Array Methods for Better, Cleaner Code


📈 24.64 Punkte
🔧 Programmierung

🔧 Javascript Array Methods – Easy Examples That Make Learning A Blast


📈 24.64 Punkte
🔧 Programmierung

🔧 4 JavaScript Array Methods Every Developer Should Master (Part 2)


📈 24.64 Punkte
🔧 Programmierung

🔧 Unleashing the Power of Customized Array Methods in JavaScript.


📈 24.64 Punkte
🔧 Programmierung

🔧 The Most Important Javascript Array Methods to Understand


📈 24.64 Punkte
🔧 Programmierung

🔧 Array Sort Methods in JavaScript.!


📈 24.64 Punkte
🔧 Programmierung

🔧 New JavaScript Array Methods to Help You Write Better, Cleaner Code


📈 24.64 Punkte
🔧 Programmierung

🔧 JavaScript Array Methods: Map vs Filter vs Redux


📈 24.64 Punkte
🔧 Programmierung

🔧 JavaScript Array Methods Cheat Sheet: The Only Guide You Need


📈 24.64 Punkte
🔧 Programmierung

🔧 JavaScript Array Methods with Practical Examples


📈 24.64 Punkte
🔧 Programmierung

🔧 Exploring JavaScript Array Methods: A Deep Dive into `.slice()` and `.splice()`


📈 24.64 Punkte
🔧 Programmierung

🔧 All the new javascript array methods you missed in 2023


📈 24.64 Punkte
🔧 Programmierung

matomo