Essential JavaScript Array Methods for Data Manipulation
Finding Element Posiitons
The indexOf() method locates the first occurrence of a specified value within an array and returns its position:
const sequence = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const position = sequence.indexOf(7);
console.log(position); // Output: 6
Combining Arrays
Arrays can be merged using the concat() method, which creates a new array with out modifying the originals:
const teamA = ["Cecilie", "Lone"];
const teamB = ["Emil", "Tobias", "Linus"];
const combinedTeams = teamA.concat(teamB);
console.log(combinedTeams); // Output: ["Cecilie", "Lone", "Emil", "Tobias", "Linus"]
const group1 = ["Jani", "Tove"];
const group2 = ["Stale", "Kai Jim", "Borge"];
const group3 = ["Cecilie", "Lone"];
const entireGroup = group1.concat(group2, group3);
console.log(entireGroup); // Output: ["Jani", "Tove", "Stale", "Kai Jim", "Borge", "Cecilie", "Lone"]
Converting Arrays to Strings
The join() method transforms array elements into a single string with optional separators:
const items = ["Banana", "Orange", "Apple", "Mango"];
const itemString = items.join();
console.log(itemString); // Output: "Banana,Orange,Apple,Mango"
Modifying Array Endpoints
Remove the final element using pop(), which returns the removed item:
const inventory = ["Banana", "Orange", "Apple", "Mango"];
const removedItem = inventory.pop();
console.log(inventory); // Output: ["Banana", "Orange", "Apple"]
console.log(removedItem); // Output: "Mango"
Add elements to the end with push():
inventory.push("Grape");
console.log(inventory); // Output: ["Banana", "Orange", "Apple", "Grape"]
Reversing Array Order
The reverse() method inverts the sequence of elements:
inventory.reverse();
console.log(inventory); // Output: ["Grape", "Apple", "Orange", "Banana"]
Working with First Elements
Remove the initial element using shift(), which also returns the removed value:
const firstRemoved = inventory.shift();
console.log(inventory); // Output: ["Apple", "Orange", "Banana"]
console.log(firstRemoved); // Output: "Grape"
Insert elements at the beginning with unshift():
inventory.unshift("Dragon Fruit");
console.log(inventory); // Output: ["Dragon Fruit", "Apple", "Orange", "Banana"]
Extracting Array Segmants
Use slice() to create a shallow copy of a portion of an array:
const selection = inventory.slice(1, 3);
console.log(selection); // Output: ["Apple", "Orange"]
Array Splicing Operations
The splice() method provides flexible insertion and deletion capabilities:
const digits = [1, 2, 3, 4, 5, 6, 7, 8, 9];
// Insert element at specific position without deleting
digits.splice(2, 0, 100);
console.log(digits); // Output: [1, 2, 100, 3, 4, 5, 6, 7, 8, 9]
// Remove elements from specific position
// digits.splice(2, 3);
// console.log(digits); // Output: [1, 2, 6, 7, 8, 9]
Sorting Array Elements
Alphabetical sorting of strings:
const produce = ["Banana", "Orange", "Apple", "Mango"];
produce.sort();
console.log(produce); // Output: ["Apple", "Banana", "Mango", "Orange"]
Numerical sorting requires a comparison function:
const numericData = [5, 7, 9, 6, 1, 8, 3, 4, 2];
// Descending order
numericData.sort((a, b) => b - a);
console.log(numericData); // Output: [9, 8, 7, 6, 5, 4, 3, 2, 1]
// Ascending order
numericData.sort((a, b) => a - b);
console.log(numericData); // Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]