Array
Kalyanasundar Arunachalam
UX UI Designer | Front End Developer | Figma | ReactJS
Understanding Arrays in JavaScript:
Arrays are fundamental data structures in programming, and in JavaScript, they offer a flexible way to store and manipulate collections of data. This article provides an overview of arrays, explores the different types of arrays, and demonstrates basic operations like insertion, deletion, updating, and searching.
What is an Array?
An array is a collection of elements, each identified by an index or key. In JavaScript, arrays are zero-indexed, meaning the first element is accessed at index 0, the second at index 1, and so on. Arrays can hold elements of any data type, including numbers, strings, objects, or even other arrays.
let fruits = ['apple', 'banana', 'cherry'];
console.log(fruits[0]); // Output: apple
Types of Arrays
1. Single-Dimensional Arrays:
These arrays have only one level of elements. They are the simplest type and are often used to store a list of items.
let numbers = [1, 2, 3, 4, 5];
2. Multi-Dimensional Arrays:
These arrays have more than one level, typically used to represent matrices or grids. The most common multi-dimensional array is the two-dimensional array.
let matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
Basic Operations on Arrays
1. Insertion
To insert an element into an array, you can use the following methods:
- At the End: You can add an element to the end of an array using the push method. If you need to do this without using any methods, you can directly assign a value to the next available index.
领英推荐
let colors = ['red', 'green', 'blue'];
colors[colors.length] = 'yellow'; // Inserting 'yellow' at the end
console.log(colors); // Output: ['red', 'green', 'blue', 'yellow']
- At a Specific Position: To insert an element at a specific index, shift the existing elements and then assign the new value.
let animals = ['cat', 'dog', 'rabbit'];
let index = 1;
let newAnimal = 'hamster';
for (let i = animals.length; i > index; i--) {
animals[i] = animals[i - 1];
}
animals[index] = newAnimal;
console.log(animals); // Output: ['cat', 'hamster', 'dog', 'rabbit']
2. Deletion
To remove elements from an array:
- From the End: You can use the pop method to remove the last element. Without using methods, you can simply reduce the array length.
let numbers = [1, 2, 3, 4, 5];
numbers.length = numbers.length - 1; // Remove the last element
console.log(numbers); // Output: [1, 2, 3, 4]
- From a Specific Position: Remove an element by shifting subsequent elements to the left.
let names = ['Alice', 'Bob', 'Charlie'];
let index = 1; // Index of 'Bob'
for (let i = index; i < names.length - 1; i++) {
names[i] = names[i + 1];
}
names.length = names.length - 1; // Adjust the length of the array
console.log(names); // Output: ['Alice', 'Charlie']
3. Updating
To update an element at a specific index:
let scores = [85, 90, 95];
let index = 1; // Index of the element to update
let newScore = 92;
scores[index] = newScore;
console.log(scores); // Output: [85, 92, 95]
4. Searching
To search for an element in an array:
- Linear Search: Iterate through the array and compare each element.
let numbers = [10, 20, 30, 40, 50];
let searchValue = 30;
let foundIndex = -1;
for (let i = 0; i < numbers.length; i++) {
if (numbers[i] === searchValue) {
foundIndex = i;
break;
}
}
console.log(foundIndex); // Output: 2 (index of 30)