Introduction to Arrays Array - It is similar to our ordinary object function and is also used to store some values The difference is that ordinary objects use strings as attribute values, while arrays use numbers as index operation elements. Index: integer starting from 0 The storage performance of arrays is better than that of ordinary objects. In development, we often use arrays to store some data. Create an array: Adding elements to an array Syntax: array[index] = value Read elements from an array Syntax: array[index] If you read a non-existent index, it will not report an error but return undefined Get the length of an array You can use Syntax: For continuous arrays, use For non-contiguous arrays, using Try not to create non-contiguous arrays. Modify length If the modified length is less than the original length, the extra part will be left blank. If the modified length is less than the original length, the extra elements will be deleted. Add an element to the last position of an array Syntax: arr[arr.length] = 70; arr[arr.length] = 80; arr[arr.length] = 90; Array literals Creating arrays using array literals grammar: When creating an array using a literal, you can specify the elements in the array when creating it. var arr = [1,2,3,4,5]; When using a constructor to create an array, you can also add elements at the same time. Pass the elements to be added as parameters of the constructor, and separate the elements with var arr = new Array(1,2,3,4,5); Notice: Use [] to create an array with an element 10 var arr = [10]; When using the constructor to create an array with one parameter, an empty array with a length of 10 is created; var arr = new Array(10); console.log(arr); console.log("arr.length="+arr.length); The array can contain any data type. var arr = ["Sun Wukong", 1, true, null, undefined]; console.log(arr); Can be an object var arr = [{name:"Sun Wukong"}, {name:"Zhu Bajie"}, {name:"Sha Wujing"}]; console.log(arr[0].name); Can be a function var arr = [ function () { alert(1) }, function () { alert(2) }]; Calling functions via Two-dimensional array create: use var arr = [[1,2,3],[4,5,6],[7,8,9]]; //3 rows and 3 columns of a two-dimensional array Using var a = new Array( new Array(10,20,30), new Array(11,22,33), new Array(45,56,67) ) Element access array name [row subscript] [column subscript] (1) Transpose of a two-dimensional array: var a = [ ['a','b','c'], ['d','e','f'], ['g','h','i'], ['i','k','I'] ] var str = '' for(var i=0;i<a.length;i++){ for(var j=0;j<a[i].length;j++){ str += a[i][j]+'\t'; } str += '\n'; } console.log("Before transposition:\n",str); var res = [] for(var i=0;i<a[0].length;i++){ res[i] = [] for(var j=0;j<a.length;j++){ res[i][j] = a[j][i]; } } console.log("After transposition:",res); (2) Define a two-dimensional array and output the maximum value of each row of the array var str = '' for(var i=0;i<a.length;i++){ //Outer loop: a.length represents the number of rows in a two-dimensional array for(var j=0;j<a[i].length;j++){ //Inner loop: a[i].length represents the number of elements (columns) in row i str += a[i][j]+'\t' } str += '\n'; //Add a newline character at the end of each line} console.log(str); for(var i=0;i<a.length;i++){ var max = a[i][0] for(var j=1;j<a[i].length;j++){ if(max<a[i][j]){ max = a[i][j]; } } console.log("The maximum value of the line "+(i+1)+" is: "+max) } SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Example code for implementing raindrop animation effect with CSS
>>: Douban website's method for making small changes to website content
student.xml <?xml version="1.0" enco...
First, what is database partitioning? I wrote an ...
As the application of centos on the server side b...
Object's hasOwnProperty() method returns a Bo...
1.docker search mysql查看mysql版本 2. docker pull mys...
Table of contents Example 1 Example 2 Example 3 E...
Building an image is a very important process in ...
A dynamic clock demo based on Canvas is provided ...
In the previous article, you have installed Docke...
This article example shares the specific code of ...
Introduction MySQL achieves high availability of ...
Recently, when I installed MySQL in Docker, I fou...
Install the latest stable version of MySQL on Lin...
Front-end project packaging Find .env.production ...
Mini Program Custom Scroll-View Scroll Bar Withou...