search
HomeWeb Front-endJS TutorialProperties 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>> ;
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)

<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)</script>

var d = c.splice(2,2) //c is [1, 2, 5] d is [3, 4]

var e = new Array(1, 2, 3, 4, 5)
var f = f.splice(-4,2) //e is [1, 4, 5] f is [2, 3]
var g = new Array(1, 2, 3, 4, 5)
var h = g.splice(-2,-2) //The second parameter represents the length, so negative numbers are invalid here

var i = new Array(1 , 2, 3, 4, 5)
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
var k = new Array(1, 2, 3, 4, 5)
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




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
JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.