javascript is not an array method

PHPz
Release: 2023-05-12 12:37:07
Original
513 people have browsed it

In JavaScript, arrays are a very common data structure. They are widely used to store and manipulate ordered collections of data. Although there are many useful methods in arrays, some may wonder why some of these methods (or properties) appear to belong to the array, but are actually methods (or properties) of the Array object rather than methods (or properties) of the array itself. Attributes). In this article, we will discuss some common misconceptions and common mistakes and explain array methods and properties in JavaScript.

What is an array?

Let us first understand what an array is. An array in JavaScript is an ordered list that can store various types of data, including numbers, strings, objects, etc. For example, the following is a simple array:

let myArray = [1, 2, 3, "four", { five: 5 }];
Copy after login

In this array, we have some numbers, a string, an object, and they are all stored in the array in a specific order. In order to manipulate and access elements in an array, JavaScript provides a set of built-in methods and properties that allow developers to easily perform various operations on the array.

Which methods are not array methods?

Listed below are some common Array object methods. They look like array methods, but are actually Array object methods because they can be used on any object, not just arrays:

join()

The Array.join() method joins all elements in the array into a string. Specifically, this method concatenates all elements into a string and inserts a specified string as a separator between them. Example:

let myArray = ["a", "b", "c", "d"];
let joinedArray = myArray.join(",");
console.log(joinedArray); // "a,b,c,d"
Copy after login

indexOf() and lastIndexOf()

Array.indexOf() method returns the index of the first element found in the array. If the element is not found in the array, it returns -1. The Array.lastIndexOf() method starts searching from the end of the array and returns the index of the last element found. If the element is not found in the array, it returns -1. Example:

let myArray = [1, 2, 3, 2];
let index = myArray.indexOf(2); // 返回 1
let lastIndex = myArray.lastIndexOf(2); // 返回 3
Copy after login

toString()

Array.toString() method converts an array into a string and returns the string. This method is similar to the Array.join() method, but uses commas as separators to join elements in the array. Example:

let myArray = [1, 2, 3];
let strArray = myArray.toString(); // 返回 "1,2,3"
Copy after login

slice()

Array.slice() method returns a new array that contains the array elements from the start index to the end index (excluding the end index). Example:

let myArray = [1, 2, 3, 4, 5];
let newArray = myArray.slice(2, 4); // 返回 [3, 4]
Copy after login

concat()

Array.concat() method concatenates two or more arrays and returns a new array. Example:

let firstArray = [1, 2, 3];
let secondArray = [4, 5, 6];
let newArray = firstArray.concat(secondArray); // 返回 [1, 2, 3, 4, 5, 6]
Copy after login

push() and pop()

The Array.push() and Array.pop() methods are used to add an element to the end of the array and delete one from the end of the array respectively. element. Example:

let myArray = ["one", "two"];
myArray.push("three"); // 返回 3,数组变成 ["one", "two", "three"]
let lastElement = myArray.pop(); // 返回 "three",数组变成 ["one", "two"]
Copy after login

shift() and unshift()

The Array.shift() and Array.unshift() methods are used to delete an element from the beginning of the array and add to the beginning of the array respectively. an element. Example:

let myArray = ["one", "two"];
myArray.unshift("zero"); // 返回 3,数组变成 ["zero", "one", "two"]
let firstElement = myArray.shift(); // 返回 "zero",数组变成 ["one", "two"]
Copy after login

The difference between array methods and methods of Array objects

Although the methods listed above look like array methods, the most important difference is that they do not modify the original array. itself. Instead, they return a new array, string, or primitive type value. For example, in the following example, we demonstrate the difference of the slice() method. Note that the original array is not changed, but a new array is created.

let myArray = ["a", "b", "c", "d", "e", "f"];
let newArray = myArray.slice(2, 4); // 返回 ["c", "d"]
console.log(myArray); // ["a", "b", "c", "d", "e", "f"]
Copy after login

In contrast, if we use the push() method of the array, the method will modify the array itself and will add a new element.

let myArray = ["a", "b", "c"];
myArray.push("d"); // push() 方法将修改原数组本身
console.log(myArray); // ["a", "b", "c", "d"]
Copy after login

Additionally, array methods such as push(), pop(), shift(), and unshift() change the original array and return a changed array, while Array object methods such as concat() , join(), indexOf() and slice()) returns a new array, a string, or a value of a primitive type, and does not change the original array.

Conclusion

Although arrays in JavaScript have many useful methods, it should be noted that some methods that appear to belong to arrays are actually methods of Array objects. Although these methods can be used on any object, they do not modify the original object and the result returned is a new array, string, or primitive type value. In order to better understand arrays and Array objects in JavaScript, developers need to clearly understand the difference between the two.

The above is the detailed content of javascript is not an array 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template