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

Explanation on Arrays and Arrays method

Patricia Arquette
Patricia ArquetteOriginal
2025-01-21 00:32:09635browse

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>
  • Use Array constructor:
<code class="language-javascript">   const anotherArray = new Array(5); // 创建一个包含5个空槽的数组
   const yetAnotherArray = new Array(1, 2, 3); </code>

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>

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>

Commonly used array methods:

  • push(): Add one or more elements to the end of the array.
<code class="language-javascript">   fruits.push("mango"); </code>
  • pop(): Removes the last element of the array and returns it.
<code class="language-javascript">   const removedFruit = fruits.pop(); </code>
  • unshift(): Adds one or more elements to the beginning of the array.
<code class="language-javascript">   fruits.unshift("kiwi"); </code>
  • shift(): Deletes the first element of the array and returns it.
<code class="language-javascript">   const firstFruit = fruits.shift(); </code>
  • 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>
  • 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>
  • concat(): Creates a new array by concatenating existing arrays.
<code class="language-javascript">   const combinedFruits = fruits.concat(["pineapple", "strawberry"]); </code>
  • join(): Concatenates all array elements into a string, separated by the specified delimiter.
<code class="language-javascript">   const fruitString = fruits.join(", "); </code>
  • indexOf(): Returns the first index of the given element.
<code class="language-javascript">   const index = fruits.indexOf("apple"); </code>
  • includes(): Checks whether the array contains an element.
<code class="language-javascript">   const hasBanana = fruits.includes("banana"); </code>
  • forEach(): Execute the provided function once for each array element.
<code class="language-javascript">   fruits.forEach(fruit => console.log(fruit)); </code>
  • 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>
  • 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>

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!

Statement:
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
Previous article:Utilizing Promise.all()Next article:Utilizing Promise.all()