JavaScript is a commonly used programming language that is widely used in web development and application development. Among them, array is one of the commonly used data structures in JavaScript. It can store a set of related data and support various operations to process this data. JavaScript provides many methods that can be used for array operations, and this article will introduce some of them.
push() method allows us to add one or more elements to the end of the array and returns the updated array length. For example, the following code will add an element to the end of the array ages:
let ages = [25, 30, 35]; ages.push(40); console.log(ages); // [25, 30, 35, 40]
The pop() method is the opposite of the push() method. Removes an element from the end of an array and returns the removed element. For example, the following code will delete the last element of the array ages:
let ages = [25, 30, 35]; let deletedAge = ages.pop(); console.log(deletedAge); // 35 console.log(ages); // [25, 30]
The shift() method is similar to the pop() method, but it Removes the first element from the beginning of the array and returns the removed element. For example, the following code will delete the first element of the array ages:
let ages = [25, 30, 35]; let deletedAge = ages.shift(); console.log(deletedAge); // 25 console.log(ages); // [30, 35]
The unshift() method is the opposite of the push() method. Adds one or more elements from the beginning of the array and returns the updated array length. For example, the following code will add an element to the beginning of the array ages:
let ages = [25, 30, 35]; ages.unshift(20); console.log(ages); // [20, 25, 30, 35]
slice() method allows us to create an element from an array New array containing elements between the specified start and end indices. For example, the following code will create a new array containing the second and third elements in the array ages:
let ages = [25, 30, 35]; let newAges = ages.slice(1, 3); console.log(newAges); // [30, 35]
splice The () method allows us to add, remove or replace one or more elements from an array. It accepts three parameters: starting index, number of elements to remove, and elements to add. For example, the following code will delete the second element in the ages array and add a new element at the same position:
let ages = [25, 30, 35]; ages.splice(1, 1, 28); console.log(ages); // [25, 28, 35]
let ages = [25, 30, 35]; let index = ages.indexOf(35); console.log(index); // 2
let ages = [25, 30, 35]; ages.forEach(function(age) { console.log("The person's age is " + age); });
let ages = [25, 30, 35]; let doubledAges = ages.map(function(age) { return age * 2; }); console.log(doubledAges); // [50, 60, 70]
let ages = [25, 30, 35]; let filteredAges = ages.filter(function(age) { return age > 30; }); console.log(filteredAges); // [35]
The above is the detailed content of Arrays in JavaScript involve methods. For more information, please follow other related articles on the PHP Chinese website!