Home > Web Front-end > JS Tutorial > Explanation on Arrays and Arrays method

Explanation on Arrays and Arrays method

Patricia Arquette
Release: 2025-01-21 00:32:09
Original
684 people have browsed it

Explanation on Arrays and Arrays method

Detailed explanation and common methods of JavaScript arrays:

What is an array?

In JavaScript, an array is a special object used to store a series of values ​​(elements) under a variable name. The values ​​can be of different data types (numbers, strings, booleans, objects, or even other arrays).

Main features:

  • Ordered: The elements in the array have a specific order, with their positions indexed starting from 0.
  • Mutable: Once an array is created, its elements can be changed.
  • Dynamic: Arrays can grow or shrink in size as needed.

Create array:

  • Literal representation:
<code class="language-javascript">   const myArray = [1, "hello", true, null]; </code>
Copy after login
  • Use Array constructor:
<code class="language-javascript">   const anotherArray = new Array(5); // 创建一个包含5个空槽的数组
   const yetAnotherArray = new Array(1, 2, 3); </code>
Copy after login

Access array elements:

Use square bracket notation and indexing:

<code class="language-javascript">   const fruits = ["apple", "banana", "orange"];
   console.log(fruits[0]); // 输出: "apple" (第一个元素)
   console.log(fruits[2]); // 输出: "orange" (第三个元素)</code>
Copy after login

Modify array elements:

Assign the new value to the desired index:

<code class="language-javascript">   fruits[1] = "grape"; 
   console.log(fruits); // 输出: ["apple", "grape", "orange"]</code>
Copy after login

Commonly used array methods:

  • push(): Add one or more elements to the end of the array.
<code class="language-javascript">   fruits.push("mango"); </code>
Copy after login
  • pop(): Removes the last element of the array and returns it.
<code class="language-javascript">   const removedFruit = fruits.pop(); </code>
Copy after login
  • unshift(): Adds one or more elements to the beginning of the array.
<code class="language-javascript">   fruits.unshift("kiwi"); </code>
Copy after login
  • shift(): Deletes the first element of the array and returns it.
<code class="language-javascript">   const firstFruit = fruits.shift(); </code>
Copy after login
  • slice(): Creates a shallow copy of a portion of the array.
<code class="language-javascript">   const citrusFruits = fruits.slice(1, 3); // 从索引1到2(不包括2)的元素</code>
Copy after login
  • splice(): Add/remove array elements at the specified position.
<code class="language-javascript">   fruits.splice(1, 0, "pear"); // 在索引1处插入"pear"
   fruits.splice(2, 1); // 从索引2处删除1个元素</code>
Copy after login
  • concat(): Creates a new array by concatenating existing arrays.
<code class="language-javascript">   const combinedFruits = fruits.concat(["pineapple", "strawberry"]); </code>
Copy after login
  • join(): Concatenates all array elements into a string, separated by the specified delimiter.
<code class="language-javascript">   const fruitString = fruits.join(", "); </code>
Copy after login
  • indexOf(): Returns the first index of the given element.
<code class="language-javascript">   const index = fruits.indexOf("apple"); </code>
Copy after login
  • includes(): Checks whether the array contains an element.
<code class="language-javascript">   const hasBanana = fruits.includes("banana"); </code>
Copy after login
  • forEach(): Execute the provided function once for each array element.
<code class="language-javascript">   fruits.forEach(fruit => console.log(fruit)); </code>
Copy after login
  • map(): Creates a new array by applying a function to each element of the original array.
<code class="language-javascript">   const fruitLengths = fruits.map(fruit => fruit.length); </code>
Copy after login
  • filter(): Creates a new array containing only elements that pass the test provided by the function.
<code class="language-javascript">   const longFruits = fruits.filter(fruit => fruit.length > 5); </code>
Copy after login

This is a basic overview of JavaScript arrays and their methods. There are many other methods available, each with its own specific purpose. Hope this helps!

The above is the detailed content of Explanation on Arrays and Arrays method. 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template