Home Web Front-end HTML Tutorial Detailed explanation of HTML array_html/css_WEB-ITnose

Detailed explanation of HTML array_html/css_WEB-ITnose

Jun 24, 2016 am 11:45 AM

Array

1. Array declaration:

1. One-dimensional array

var x=new Array();//Empty array

var x = new Array(size); //Array of specified length

var x = new Array(element 0,element 1,...); //Specify initial value,,,, does not limit the data type stored

Also: var x =["Xiao Ming", "Xiao Hong"];//Indicates one-dimensional array array elements----Xiao Ming, Xiao Hong

3] means that the length of the array x is 3; = {'you', 'good', 'ah'};

arr[1] = {'you', 'true', 'beautiful'};

arr[2] = {'you' , 'are' , 'full'};

//Traverse the two-dimensional array

for(var i=0; i<arr.length; i )

{

for(var j=0; i<arr[i].length ; i )

Document.write(arr[i] [j] ” ”);

}

Document.write('<hr/>');

}

2. The length and subscript of the array

1. var a = new Array(); define a null pointer

But a subscript that exceeds the length of the array means changing the length of the array

2. a.length=5 (adding numbers directly changes the length------can be lengthened or shortened)

But alert (a[6]) will display undefined

3. Commonly used methods for arrays

Methods

Function

document.write(arr.slice(2,-2));
EG

concat

Concatenates two or more arrays and returns the result
var c=a.concat(b);

Join

Convert all elements of the array Put a character into the element
Var a=new Array(''a,'b'); a.join('"!")

pop

Remove and return the last element of the array
a.pop();

Push

Adds one or more elements to the end of the array and returns the new length
Returns the length

Reverse

Reverse the order in the array
Return new array after inversion

Shift

Delete and return array The first element of
removes and returns the removed element

Slice

Return selected elements from an existing array

var arr = ["George","John","Thomas","James" ,"Adrew","Martin"];

//Thomas,James//Negative numbers count from the back to -2 (i.e. the third to last number)

Sort()

Sort the elements of the array
The default is to sort according to the order of character encoding

Parameter writing comparison function

splice

Delete elements and add new elements to the array

1. splice(index deleted position Negative numbers count backwards, howmany quantity, new1, add from deleted position, new2...)

tostring

Convert the array to a string and return the result
is designed for strings and automatically adds , number

Unshift

Adds one or more elements to the beginning of the array and returns the new length

                                                                                                                                

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv ="Content-Type" content="text/html; charset=utf-8" />
<title>array</title>
<script type="text/javascript">
var a = new Array();
var name = prompt('Please enter a string, if you want to end, please enter "END" to end', 'END');//'END ' is the default filled content
a[a.length] = name;
} while (name != 'END');
a.length = a.length - 1;
alert( a);


var b = new Array();
b[0] = ["ni", "hao"];
b[1] = [3, 4 ];
//for (var i = 0; i < b.length; i )
// for (var j = 0; j < b[i].length; j )
// { // document.write (b [i] [j] "& lt; br/& gt;")
//}
for (var I in b)
for (var j in b [i]) <[> {
Document.write (b [i] [j] "& lt; br/& gt;")
}
var b = new array ', 'overload', 'hit');


for (var i = 0; i < b.length; i ) {
alert(b[i]);
                                                                                                                
                   
document.write(b.join("!"));
document.write("<br/>");


//pop() deletes the last An element and output the deleted array
document.write(b.pop());


//tostring() method;
var arr = new Array();
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"
document.write(arr "<br/>");


//for(var i in arr :) toString();
document.write(c "<br/>");
document.write(c.replace(/,/g, "") "<br/>") ;//Remove after toString, number /,/g without adding g means to remove the first
document.write(arr "<br />")


//Sort
        document.write(arr.sort());


                                                                                                                                                                                                 .
document.write(arr.shift() "<br />");
document.write(arr);
//
shift
document.write(arr. shift());
document.write(arr);
for (var i in arr) {
document.write(i arr[i]);


}
Demo of two-dimensional array
for (var i = 0; i < arr.length; i ) {
for (var j = 0; j < arr[i].length; i ) {
                document.write(arr[i][j] "  ");
            }
            document.write('<hr/>');
        }


    </script>
</head>
<body>


</body>
</html>

 

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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

How do I use HTML5 form validation attributes to validate user input?

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

How to efficiently add stroke effects to PNG images on web pages?

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

What is the purpose of the <iframe> tag? What are the security considerations when using it?

What are the security implications of using iframes, and how can I mitigate them? What are the security implications of using iframes, and how can I mitigate them? Mar 18, 2025 pm 02:51 PM

What are the security implications of using iframes, and how can I mitigate them?

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

What are the best practices for cross-browser compatibility in HTML5?

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

How do I use the HTML5 <time> element to represent dates and times semantically?

How do I use the HTML5 <meter> element to display numerical data within a range? How do I use the HTML5 <meter> element to display numerical data within a range? Mar 12, 2025 pm 04:08 PM

How do I use the HTML5 <meter> element to display numerical data within a range?

See all articles