Detailed explanation of built-in object Array in JS
This article mainly shares with you a detailed explanation of the built-in object Array in JS. The built-in objects of JS are mainly divided into four categories: Array String Math Date. We mainly introduce Array, hoping to help everyone.
Array
Each item in the array can save any type of data, and the size of the array can be adjusted as needed.
1. Create
a. Use the constructor:
var colors= new Array( ) The brackets can be the number of data in the array, or each item Specific data, but it should be noted that each item of data is separated by commas.
b. Array literal representation
var colors=[1,2,3] Separate each item of data with commas.
2. The access and setting of the element
[] starts from 0. If the length of the array is exceeded, the return value is undefined.
3. Length
array.length, the return value is number.
’ ’ s Str through ’ ’ ’s ’ ’s way through ’ through through through array.length ’ s through ’ through ’ through ’ ’s ’ through through ‐ through ‐‐ ‐‐ ‐ _ _ _ array.length, the return value is number.
# When placing a position beyond the size of the current array, the system will automatically re -calculate the length of the array, and the length value is added to the last index.
4. Traversal
for(var i=0;i<array.length;i++) { console.log(array[i]); }
5. Some commonly used methods in arrays
a. push( ) arrayObject.push( value1,value2...)
‐ ... value2 ...)
Add the value in the brackets in order to ArrayObject head
rebate the new length of the array
C. POP ( ) arrayObject.pop( ) Delete the last element and return the deleted element
d. shift( ) arrayObject.shift( ) Delete the first element and return the deleted element
e. join( ) arrayObject.join(separator)
Put the elements in the array into a string and return a string; the separator defaults to comma, and the default space is not written.
f. sort( ) arrayObject.sort( )
Sorts the array elements, and the return value is the array
Even if each in the group The items are all numbers. The comparison of this method is still a string. Sort in the order of the string
# This can receive a comparison function as a parameter
Array.sort (function (x, y) {
through
return y-x; // 降序输出
})
g. reverse( ) arrayObject.reverse( ) 将数组中的元素颠倒然后输出,返回数组
h. concat( ) arrayObject.concat(数组1,数组2...... ) 连接两个或者多个数组,返回数组
arr3=arr1.concat(arr2) 1连接2然后放到3里面
i. slice( ) arrayObject.slice( start ,end ) 返回数组选定的元素 返回值为数组
start:必需,从哪开始,可以是负数,负数+数组的长度就是start
end:可选,截止位置,不写默认到最后一个字符
截取的元素从start开始,到end-1结束
[例题]:var a=[1,'yes',3] b; 请做b对a的复制,方法越多越好
方法一:数组遍历
b=new Array(); for( var i=0;i<a.length;i++) { b.push(a[i]); }
方法二: 使用concat( )
b=[].concat(a);
方法三:使用slice( )
b=a.slice(0);
j. splice( )
<1> 删除 arrayObject.splice( index,count)
删除从index处开始的count个元素,返回被删除元素的数组,count为0时,不做任何操做,count不设置值时,从index后的所有元素都将被删除。
<2> 插入 arrayObject.splice( index,0,value1,value2......)
从index位置插入value的值,返回数组。在索引的后一位插入数据
<3> 替换 arrayObject.splice( index,count,value1,value2......)
返回值:从原始数组中删除的数据,没删除返回空数组, count为要删除的项数
k. indexOf( ) arrayObject.indexOf( searchvalue, startIndex)
searchvalue:必需,所要查找的数据
Startindex: Optional, starting point, the default is 0
This return value: Number, the item of the back search is in the array position, no return-1
## l. lastIndexOf( ) is similar to the previous one, starting from the last element of the array.
php custom two-dimensional array sorting function array
###The above is the detailed content of Detailed explanation of built-in object Array in JS. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of Linux's curl command Summary: curl is a powerful command line tool used for data communication with the server. This article will introduce the basic usage of the curl command and provide actual code examples to help readers better understand and apply the command. 1. What is curl? curl is a command line tool used to send and receive various network requests. It supports multiple protocols, such as HTTP, FTP, TELNET, etc., and provides rich functions, such as file upload, file download, data transmission, proxy

Detailed explanation of Promise.resolve() requires specific code examples. Promise is a mechanism in JavaScript for handling asynchronous operations. In actual development, it is often necessary to handle some asynchronous tasks that need to be executed in sequence, and the Promise.resolve() method is used to return a Promise object that has been fulfilled. Promise.resolve() is a static method of the Promise class, which accepts a

Numpy is a Python scientific computing library that provides a wealth of array operation functions and tools. When upgrading the Numpy version, you need to query the current version to ensure compatibility. This article will introduce the method of Numpy version query in detail and provide specific code examples. Method 1: Use Python code to query the Numpy version. You can easily query the Numpy version using Python code. The following is the implementation method and sample code: importnumpyasnpprint(np
