A brief introduction to JavaScript arrays

A brief introduction to JavaScript arrays

Introduction to Arrays

Array - Array array is also an object

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:

var arr = new Array();
When you use typeof to check an array, it returns object.

insert image description here

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 length property to get the length of the array (the number of elements)

Syntax:數組.length

For continuous arrays, use length to get the length of the array (the number of elements)

For non-contiguous arrays, using length will get the maximum index of the array + 1

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:數組[數組.length] = 值

arr[arr.length] = 70;
arr[arr.length] = 80;
arr[arr.length] = 90;

Array literals

Creating arrays using array literals

grammar: []

var arr = [] ;

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];

insert image description here

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);

insert image description here

The array can contain any data type.

var arr = ["Sun Wukong", 1, true, null, undefined];
console.log(arr);

insert image description here

Can be an object

var arr = [{name:"Sun Wukong"}, {name:"Zhu Bajie"}, {name:"Sha Wujing"}];
console.log(arr[0].name);

insert image description here

Can be a function

var arr = [
    function () { alert(1) },
    function () { alert(2) }];

Calling functions via arr[0]()

insert image description here

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 new Array

  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);

insert image description here

(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)
}

insert image description here

Summarize

This 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:
  • Using for loop to traverse array in JavaScript
  • Problems in deleting array elements in a for loop in JavaScript
  • Detailed explanation of for loop and double for loop in JavaScript
  • JavaScript for loop performance test example
  • In-depth understanding of the for loop in JavaScript
  • Commonly used JavaScript array methods
  • JavaScript basics for loop and array

<<:  Example code for implementing raindrop animation effect with CSS

>>:  Douban website's method for making small changes to website content

Recommend

Combining XML and CSS styles

student.xml <?xml version="1.0" enco...

Detailed explanation of mysql partition function and example analysis

First, what is database partitioning? I wrote an ...

Detailed tutorial on installing PHP and Nginx on Centos7

As the application of centos on the server side b...

Use of hasOwnProperty method of js attribute object

Object's hasOwnProperty() method returns a Bo...

Docker deploys mysql to achieve remote connection sample code

1.docker search mysql查看mysql版本 2. docker pull mys...

Share JS four fun hacker background effect codes

Table of contents Example 1 Example 2 Example 3 E...

JS+Canvas realizes dynamic clock effect

A dynamic clock demo based on Canvas is provided ...

How to deploy your first application with Docker

In the previous article, you have installed Docke...

jQuery custom magnifying glass effect

This article example shares the specific code of ...

A brief talk about MySQL semi-synchronous replication

Introduction MySQL achieves high availability of ...

Solve the problem of inconsistency between mysql time and system time in docker

Recently, when I installed MySQL in Docker, I fou...

Installation tutorial of the latest stable version of MySQL 5.7.17 under Linux

Install the latest stable version of MySQL on Lin...

How to deploy the crownblog project to Alibaba Cloud using docker

Front-end project packaging Find .env.production ...

WeChat applet custom scroll-view example code

Mini Program Custom Scroll-View Scroll Bar Withou...