Properties and methods of Array in JavaScript.
There are four ways to define arrays
Use the constructor:
var a = new Array();
var b = new Array(8);
var c = new Array("first", "second", "third ");
or array literal:
var d = ["first", "second", "third"];
Attribute
Array has only one attribute, which is length, and length represents the memory space occupied by the array. number, not just the number of elements in the array. In the array just defined, the value of b.length is 8
<script><br>var a = new Array("first", "second", "third ")<br>a[48] = "12"<br>document.write(a.length)<br>//The displayed result is 49<br></script>
The length attribute of the array is writable, which is a very interesting Attribute, we can use this method to intercept the array
<script><br>var a = new Array("first", "second", "third")<br>delete a[1]<br>document.write(a.length )<br>//The displayed result is 3, indicating that the length of the array cannot be changed even if deleted<br>var a = new Array("first", "second", "third")<br>a.length = 1<br>document.write(a .length)<br>//The displayed result is 1, indicating that there is only one element left<br></script>
Methods
This does not include some methods that are incompatible with IE and FF:
toString(): convert the array Convert to a string
toLocaleString(): Convert the array to a string
join(): Convert the array to a symbolically connected string
shift(): Remove an element from the head of the array
unshift() : Insert an element at the head of the array
pop(): Delete an element from the end of the array
push(): Add an element to the end of the array
concat(): Add an element to the array
slice(): Return the array's Part
reverse(): Sort the array in reverse order
sort(): Sort the array
splice(): Insert, delete or replace an array element
toString() method, toLocaleString() method has a similar function, FF The function below is exactly the same. In IE, if the element is a string, a space will be added after ",". If the element is a number, it will be extended to two decimal places. Both will change the length attribute of the string, so Considering compatibility, try not to use the toLocaleString() method.
<script><br>var a = new Array(1, 2, 3, [4, 5, [6, 7]])<br>var b = a.toString() //b is "1 in the form of a string , 2, 3, 4, 5, 6, 7"<br>var c = new Array(1, 2, 3, [4, 5, [6, 7]])<br>var d = c.toLocaleString() //d In the form of string "1, 2, 3, 4, 5, 6, 7"<br>//toString() method and toLocaleString() method can disassemble multi-dimensional arrays<br></script>
join() method All elements in the array are converted into strings and then concatenated, which is the opposite operation of String's split() method. join() uses "," as the delimiter by default. Of course, you can also specify the delimiter in the method
<script><br>var a = new Array("first", "second", "third")<br>var s = a.join("...")<br>document.write(s)<br>//The displayed result is "first...second...third"<br></script>
pop() method can be obtained from The push() method deletes several elements from the end of the array and adds an element to the end of the array. These two methods are exactly two opposite operations. Both operate on the original array, but please note that the push() method returns the length of the new array, while the pop() method returns the deleted element.
<script><br>var a = new Array(1, 2, 3)<br>var b = a.push(4,5,[6,7]) //a is [1, 2, 3, 4, 5, [6, 7]] b is 6 Note that the tbpush() method will not help you open an array<br>var c = new Array(1, 2, 3, 4, "first")<br>var d = c.pop( ) //c is [1, 2, 3, 4] d is "first" in the form of a string<br></script>
The shift() method can delete an element from the head of the array, and the unshift() method can remove several elements Adding to the head of an array, these two methods are just opposite operations. Both operate on the original array, but please note that the unshift() method returns the length of the new array, while the shift() method returns the deleted element.
<script><br>var a = new Array(1, 2, 3)<br>var b = a.unshift(4,5,[6,7]) //a is [4, 5, [6, 7 ], 1, 2, 3] b is 6 Note that the unshift() method will not help you open an array, and the order in which the values are inserted is <br>var c = new Array("first", 1, 2, 3, 4 )<br>var d = c.shift() //c is [1, 2, 3, 4] d is "first" in the form of a string<br></script>
concat() method can return a new array on the original array An array of elements is added. The elements are separated by ",". If there is an array in the element, it will be expanded and added continuously, but expansion and addition in the form of multi-dimensional arrays are not supported
<script><br>var a = new Array("first", "second", "third")<br>s = a.concat("fourth",["fifth", "sixth"],["seventh", ["eighth", "ninth"]])<br>document.write(s[7])<br>//The displayed result is "eighth, ninth", indicating that "eighth, ninth" is added in the form of an array, This is the value of s as ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", ["eighth", "ninth"]]<br></script> ;<br>slice() method returns a slice of the array, or a subarray. The parameters of slice() represent the beginning and end positions of the word array. If there is only one parameter, it means taking it from there to the end. If the parameter is negative, it means a certain position of the reciprocal. slice(start,end) //Indicates that the array starts from the subscript start (including this) to end (excluding this)</p>
<p><script><br>var a = new Array(1, 2, 3, 4 , 5)<br>var b = a.slice(3) //b is [4, 5]<br>var c = a.slice(-3) //c is [3, 4, 5]<br>var d = a. slice(1,-1) //d is [2, 3, 4]<br>var e = a.slice(-3,-1) //e is [3, 4]<br></script>
reverse( ) method sorts the array in reverse order. It does not create and return a new array, but operates on the original array
<script><br>var a = new Array("first", "second", " third")<br>a.reverse()<br>document.write(a)<br>//The displayed result is "third, second, first". At this time, the order of the array has been reversed<br></script>
sort() method The function is to sort the array. This is a very peculiar method. I don't know whether the person who created it was out of laziness or cleverness. This is a method that impressed me deeply. The parameter of the
sort() method is a function with two parameters and a return value. If the returned value is greater than zero, it means that the previous parameter is larger than the next parameter. If it is equal to zero, it is equal. If it is less than zero, it means that the previous parameter is larger than the last parameter. The latter one is smaller, and the relatively smaller parameter will appear at the front of the sort. The
sort() method operates directly on the array and also returns a value, but the two seem to be equivalent. The sort() method defaults to sorting in alphabetical order
<script><br>var a = new Array(33, 4, 111, 543)<br>a.sort(way)<br>function way(x, y){<br> if (x % 2 ==0) <br> Use with use using through out through ‐ off off off off off‐ back out ‐ if (x % 2 == 0) The function of the <br>splice() method is to insert, delete or replace an array element. It will not only modify the original array, but also return the processed content. Therefore, this method is powerful but not easy to use. method, the splice() method uses the first two parameters for positioning, and the remaining parameters represent the insertion part. <br><br><script><br>var a = new Array(1, 2, 3, 4, 5)<br>var b = a.splice(2) //a is [1, 2] b is [3, 4, 5 ]<br>var c = new Array(1, 2, 3, 4, 5)</p>var d = c.splice(2,2) //c is [1, 2, 5] d is [3, 4]<p>var e = new Array(1, 2, 3, 4, 5)<br>var f = f.splice(-4,2) //e is [1, 4, 5] f is [2, 3]<br>var g = new Array(1, 2, 3, 4, 5)<br>var h = g.splice(-2,-2) //The second parameter represents the length, so negative numbers are invalid here<br> <br>var i = new Array(1 , 2, 3, 4, 5)<br>var j = i.splice(2,2,"first","second","third") //i is [1, 2, "first", "second", "third", 5] j is [3, 4] and the latter part will automatically move forward and backward to maintain the continuity of the array<br>var k = new Array(1, 2, 3, 4, 5)<br>var l = k.splice (2,2,["first","second"],"third") //k is [1, 2, ["first", "second"], "third", 5] l is [3, 4 ] The splice() method does not expand the array, it only writes directly<br></script>

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

AI Hentai Generator
Generate AI Hentai for free.

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



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

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

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
