We will explain how to add and delete elements in an array using JavaScript, and introduce commonly used array manipulation methods.
How can I add/remove elements of an array?
Use the push and unshift methods to add elements to the array, and the pop and shift methods to remove them.
Add elements to an array
To add elements to an array, use the push method to add an element to the end of the array , or the unshift method to add an element to the beginning of the array.
push() method: This method adds an element to the end of the array.
let fruits = ["apple", "banana"];
fruits.push("orange");
console.log(fruits); // ["apple", "banana", "orange"]
unshift() method: This method adds an element to the beginning of the array.
let fruits = ["banana", "orange"];
fruits.unshift("apple");
console.log(fruits); // ["apple", "banana", "orange"]
Delete an element of an array
To remove an element from an array, use the pop() method to remove an element from the end of the array, or the shift method to remove an element from the beginning of the array .
pop() method: This method removes an element from the end of an array.
let fruits = ["apple", "banana", "orange"];
fruits.pop();
console.log(fruits); // ["apple", "banana"]
shift() method: This method deletes an element from the beginning of the array.
let fruits = ["apple", "banana", "orange"];
fruits.shift();
console.log(fruits); // ["banana", "orange"]
Commonly used methods for array operations
The table below provides examples of how to use commonly used methods in JavaScript array operations.
method name | explanation | Example of use |
---|---|---|
splice() | Delete a specific element from an array or insert a new element | let fruits = ["apple", "banana", "orange"]; fruits.splice(1,1,"lemon"); console.log(fruits); // ["apple","lemon","orange"] |
slice() | Create a new array by cutting part of an array | let fruits = ["apple", "banana", "orange"]; let newFruits = fruits.slice(1,2); console.log(newFruits); // ["banana"] |
forEach() | Extract elements of an array one by one and perform specific processing | let fruits = ["apple", "banana", "orange"]; fruits.forEach(function(item,index) { console.log(index + ':' + item); }); |
map() | Create a new array by extracting elements from an array one by one | let numbers = [1,2,3,4,5]; let doubleNumbers = numbers.map(function(num){ return num*2; }); console.log(doubleNumbers); // [2,4,6,8,10] |
filter() | Extract the elements of an array one by one, extract only those that meet the conditions, and create a new array. | let numbers = [1,2,3,4,5]; let evenNumbers = numbers.filter(function(num){ return num%2==0; }); console.log(evenNumbers); // [2,4] |
summary
The contents of this article are summarized below.
- Use the push() and unshift() methods to add elements to the array.
- Use the pop() and shift() methods to remove elements from an array.
- Use the forEach() method to retrieve each element of an array one by one and perform specific processing.
- Use the map() method to create a new array by extracting the elements of an array one by one.
- Use the filter() method to extract the elements of an array one by one and create a new array by extracting only those that meet the conditions.
Array manipulation is one of the basic skills required for programming. By using the push, pop, shift, unshift, splice, slice, forEach, map, and filter methods introduced in this article, you can perform flexible array operations.
Things I didn’t understand about array operations were explained in detail, and I was able to understand them while looking at actual sample programs!
Understanding how to add and remove elements from an array will help you program more efficiently.
Comments