DEV Community

Cover image for Array Manipulation in Javascript
Omiye Precious
Omiye Precious

Posted on • Updated on

Array Manipulation in Javascript

Arrays are the fundamental data structure in all programming languages, they are commonly used to store and manipulate data such as user inputs, API responses, and database queries. For this reason, developers must understand how to manipulate arrays in JavaScript.

This article will look at the various array manipulation methods available in JavaScript. We'll review fundamental techniques like adding and removing elements, accessing and modifying elements, and sorting arrays. We will also discuss more advanced methods like filtering, mapping, and reducing arrays.

Basic Array manipulation methods

Below are basic array manipulation methods:

Adding and removing elements:

You can add or remove elements from an array using the following methods:

  • push() - this method adds one or more elements to the end of an array.
  • pop() - this method removes the last element from an array.
  • unshift() - this method adds one or more elements to the beginning of an array.
  • shift() - this method removes the first element from an array.

Example:

let numbers = [4, 5, 6, 7, 8];

numbers.push(9); // adds 9 to the end of the array
numbers.pop(); // removes the last element (8) from the array
numbers.unshift(3); // adds 3 to the beginning of the array
numbers.shift(); // removes the first element (3) from the array

Enter fullscreen mode Exit fullscreen mode

Accessing and modifying elements:

The following methods can be used to access or modify elements in an array:

  • [] - it access elements by index.
  • slice() - it returns a new array with a portion of the original array.
  • splice() - it adds or removes elements from an array.

Example:

let colors = ['red', 'blue', 'green', 'yellow'];

console.log(colors[1]); // prints 'blue'
console.log(colors.slice(0, 2)); // returns a new array ['red', 'blue']
fruits.splice(1, 2, 'orange', 'pear'); // removes 2 elements and adds 'purple' and 'orange'
console.log(fruits); // prints ['red', 'purple', 'orange', 'yellow']

Enter fullscreen mode Exit fullscreen mode

Sorting arrays:

The sort() method can be used to sort the elements of an array. Sort() sorts the elements alphabetically by default. You can also specify a custom sort order by passing a function as an argument to sort().

Example:

let states = ['ondo', 'lagos', 'ogun', 'ekiti'];

states.sort(); // sorts in alphabetical order
console.log(states); // prints ['ekiti', 'lagos', 'ogun', 'ondo']

states.sort((a, b) => b.length - a.length); // sorts by length
console.log(states); // prints ['ekiti', 'lagos', 'ogun', 'ondo']

Enter fullscreen mode Exit fullscreen mode

You can add, remove, access, and modify elements in an array, as well as sort the elements in a custom order, by using these basic array manipulation methods in JavaScript.

Advanced array manipulation methods

JavaScript provides more advanced array manipulation methods in addition to the basic array manipulation methods. In this section, we'll look at JavaScript's advanced array manipulation methods.

Filtering arrays:

You can filter an array to create a new array with only the elements that meet a specific condition. The filter() method returns a new array containing all elements that pass the test defined by the supplied function.

Example:

let numbers = [1, 2, 3, 4, 5, 6];

let evenNumbers = numbers.filter((num) => num % 2 === 0);

console.log(evenNumbers); // prints [2, 4, 6]

Enter fullscreen mode Exit fullscreen mode

Mapping arrays:

You can use an array's elements to create a new array with the same length but different elements. The map() method returns a new array containing the results of calling a given function on each element of the calling array.

Example:

 let numbers = [1, 2, 3, 4, 5, 6];

let doubledNumbers = numbers.map((num) => num * 3);

console.log(doubledNumbers); // prints [3, 6, 9, 12, 15, 18]

Enter fullscreen mode Exit fullscreen mode

Reducing arrays:

By applying a function to each element of an array, you can reduce it to a single value. To reduce an array to a single value, the reduce() method applies a function to an accumulator and each element in the array.

Example:

 let numbers = [1, 2, 3, 4, 5, 6, 7];

let sum = numbers.reduce((acc, num) => acc + num);

console.log(sum); // prints 28
Enter fullscreen mode Exit fullscreen mode

You can filter, map, and reduce arrays in JavaScript to create new arrays or single values based on specific criteria by using these advanced array manipulation methods. These techniques are especially useful when dealing with large amounts of data or performing complex calculations.

Conclusion

Finally, arrays are essential component of JavaScript and are used to store and manipulate data. Understanding array manipulation methods is essential for working effectively with arrays. Adding and removing elements, accessing and modifying elements, sorting arrays, filtering arrays, mapping arrays, and reducing arrays were all covered in this article. Using these methods, developers can write more efficient and effective array-related code.

Top comments (0)