Home > Web Front-end > Front-end Q&A > Arrays in JavaScript involve methods

Arrays in JavaScript involve methods

PHPz
Release: 2023-05-16 09:06:07
Original
472 people have browsed it

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.

  1. push() method

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]
Copy after login
  1. pop() method

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]
Copy after login
  1. shift() method

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]
Copy after login
  1. unshift() method

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]
Copy after login
  1. slice() method

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]
Copy after login
  1. splice() method

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]
Copy after login
  1. indexOf() and lastIndexOf() methods
# The ##indexOf() method returns the index of the first occurrence of the specified element in the array. If the element does not exist, returns -1. For example, the following code will return the index of element 35 in the array ages:

let ages = [25, 30, 35];
let index = ages.indexOf(35);
console.log(index); // 2
Copy after login

lastIndexOf() method is similar to the indexOf() method, but it returns the index of the last occurrence of the specified element in the array.

    forEach() method
The forEach() method allows us to iterate through all the elements in the array and perform the specified operation on each element. For example, the following code will print a message on each element in the array ages:

let ages = [25, 30, 35];
ages.forEach(function(age) {
    console.log("The person's age is " + age);
});
Copy after login

    map() method
map() method allows us to iterate over the array all elements in and convert them to new values ​​based on certain conditions. For example, the following code will multiply each element in the array ages by 2 and create a new array:

let ages = [25, 30, 35];
let doubledAges = ages.map(function(age) {
    return age * 2;
});
console.log(doubledAges); // [50, 60, 70]
Copy after login

    filter() method
filter( ) method allows us to iterate through all elements in an array and return elements that meet certain conditions. For example, the following code will return elements greater than 30 in the array ages:

let ages = [25, 30, 35];
let filteredAges = ages.filter(function(age) {
    return age > 30;
});
console.log(filteredAges); // [35]
Copy after login
Array is a very important data structure in JavaScript, which allows us to easily handle multiple similar data. In actual programming, it is very important to master array operation methods, which can greatly improve development efficiency and code readability. This article introduces some commonly used array operation methods, hoping to be helpful to readers.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template