Fading Coder

One Final Commit for the Last Sprint

Home > Notes > Content

Essential JavaScript Array Methods for Data Manipulation

Notes May 4 56

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]

Related Articles

Skipping Errors in MySQL Asynchronous Replication

When a replica halts because the SQL thread encounters an error, you can resume replication by skipping the problematic event(s). Two common approaches are available. Methods to Skip Errors 1) Skip a...

Spring Boot MyBatis with Two MySQL DataSources Using Druid

Required dependencies application.properties: define two data sources and poooling Java configuration for both data sources MyBatis mappers for each data source Controller endpoints to verify both co...

Collecting Disk Capacity and Free Space Metrics in SQL Server

Monitoring storage consumption is a routine operational task. SQL Server exposes several way to inspect disk space, ranging from quick checks to more complete inventories that include total capacity....

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.