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

Example of how to implement keepalived+nginx high availability

1. Introduction to keepalived Keepalived was orig...

MySQL master-slave principle and configuration details

MySQL master-slave configuration and principle, f...

vue-router hook function implements routing guard

Table of contents Overview Global hook function R...

Linux sftp command usage

Concept of SFTP sftp is the abbreviation of Secur...

A brief discussion on MySql views, triggers and stored procedures

view What is a view? What is the role of a view? ...

Docker container connection implementation steps analysis

Generally speaking, after the container is starte...

Detailed explanation of Nginx rewrite jump application scenarios

Application scenario 1: Domain name-based redirec...

Innodb system table space maintenance method

Environmental Description: There is a running MyS...

Summary of basic usage of $ symbol in Linux

Linux version: CentOS 7 [root@azfdbdfsdf230lqdg1b...

Vue implements small search function

This article example shares the specific code of ...

Two ways to start Linux boot service

Table of contents rc.local method chkconfig metho...

mysql security management details

Table of contents 1. Introduce according to the o...

Tutorial on logging into MySQL after installing Mysql 5.7.17

The installation of mysql-5.7.17 is introduced be...