An article to help you learn more about JavaScript arrays

An article to help you learn more about JavaScript arrays

Array: An ordered set of data

1. The role of array:

Can store multiple data at one time

2. Definition of array:

1. Create an array through the constructor

grammar:

var array name = new Array();
var array = new Array(); // defines an array

//Define an array through the constructor var array = new Array(); //Empty array with no data

If the data in the array is output directly, then the data in the array can be displayed directly; if there is no data, the data cannot be seen.

var array name = new Array(length);

If there is no data in the array but there is a length, each value in the array is undefined.

When creating an array using the constructor, if it is Array (a number) → it refers to the length of the index array (that is, the number of array elements); if it is Array (multiple values) → it means that there is data in the array, and the length of the array is the number of these data.

2. Create an array using literals

grammar:

var array name = []; // empty array

var arr = [];
console.log(arr); //Array[0]

Summarize:

Whether the array is defined by constructor or literal, if it has a length, it defaults to undefined.

Array Elements

Each data stored in an array can be called an element of the array.

For example: if 3 arrays are stored in an array, then this array has 3 elements

4. Array length

The length of the array is the number of elements

For example: if there are 3 elements, then the length of the array is 3.

var arr1 = new Array(); //Use the constructor to create an empty array var arrr2 = new Array(5); //Use the constructor to create an array of length 5, which has 5 values ​​and all are undefined
var arr3 = new Array(10,20,30,40,50); // The constructor creates an array of length 5, each value has a meaning

5. Array index (subscript)

The index of an array, or the subscript of an array. It is used to store or access the data in my array, starting from 0 and ending with length - 1

How to set the value of a position in an array:

array name[subscript]=value;

For example: arr[3]=100;

How to get the value at a certain position in an array:

var result=array name[subscript];

console.log(result);

//Get the 4th value in the array var arr =new Array(10,20,30,40,100);
console.log(arr[4]); //100
//Change the value of subscript 3 to 1000
arr[3]=1000;
//Get the length of the array using literal value var arr1=[10,20,30,40,50,12];
console.log(arr.length); //6

The relationship between array length and index:

length - 1 = index

6. Issues to note when using arrays

1. The data stored in the array can be different

arr=[10,"哈哈",true,null,undefined,new Object()];

2. The length of the array can be changed

var arr = [];
    // Set the value of an element in an array by index arr[0] = 10;
    arr[1] = 20;
    console.log(arr.length); //2
    // Get the value of the element by index console.log(arr[2]); //undefined

3. In summary

var arr1 = new Array(); //empty array var arr2 = new Array(5); //array of length 5, each array value is undefined
var arr3 = new Array(1, 2, 3, 4, 5); //Array of length 5, each value has meaning var arr4 = []; //Empty array var arr5 = [1, 2, 3]; //Array of length 3 var arr6 = ["red", "blue", 1, true]; //Array of length 4, element data types are different var arr7 = [];
//Declare an empty array and add value to the array arr7[0] = 10;
arr7[1] = 30;

7. Traversing the array

1. Positive order traversal

var arr=[10,20,30,40,50,60,70,80,90,100];
// i represents the subscript of the array // The subscript of the last number is equal to the length of the array minus one for(var i=0;i<arr.length;i++){
    console.log(arr[i]);
}

2. Reverse order traversal

var arr = [10, 20, 30, 40, 100];
// i starts to output the last number, and ends the loop when it reaches 0 for (var i = arr.length - 1; i >= 0; i--) {
    console.log(arr[i]);
}

8. Common cases in arrays

1. Find the sum of all elements in an array

var arr = [10, 20, 30, 40, 100];
// Used to store the sum after addition var sum = 0;
 for (var i = 0; i < arr.length; i++) {
     sum += arr[i];
 }
 console.log(sum); //200

2. Find the average of an array

var arr = [10, 20, 30, 40, 100];
// Used to store the sum after addition var sum = 0;
for (var i = 0; i < arr.length; i++) {
    sum += arr[i];
}
console.log(sum / arr.length); //40

3. Find the maximum and minimum values ​​of an array

var arr = [10, 20, 30, 40, 100];
// Assume that the first number in the array is the largest and assign it to a variable var max = arr[0];
// Assume that the first number in the array is the smallest and assign it to a variable var min = arr[0];
for (var i = 0; i < arr.length; i++) {
    //Compare the value in the array with max. If it is greater than max, assign that number to max
    if (max < arr[i]) {
        max = arr[i];
    }
    //Compare the value in the array with min. If it is smaller than min, assign that number to min.
    if (min > arr[i]) {
        min = arr[i];
    }
}
console.log(max); //100
console.log(min); //10

4. Bubble Sort

var arr = [10, 30, 20, 65, 40, 8, 100];
//Sort from small to large for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < arr.length - i; j++) {
        if (arr[j] > arr[j + 1]) {
            var temp = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = temp;
        }
    }
}
console.log(arr);
//Sort from large to small for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < arr.length - i; j++) {
        if (arr[j] < arr[j + 1]) {
            var temp = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = temp;
        }
    }
}
console.log(arr);

Bubble Sort

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • Learn the common methods and techniques in JS arrays and become a master
  • In-depth study of JavaScript array deduplication problem
  • Detailed explanation of several methods of deduplication in Javascript array
  • Detailed explanation of JavaScript array deduplication
  • Summary of examples of common methods of JavaScript arrays
  • Detailed explanation of the deep and shallow cloning principles of JavaScript arrays and non-array objects

<<:  How to use DCL to manage users and control permissions in MySQL

>>:  Analysis of the configuration process of installing mariadb based on docker

Recommend

Mobile front-end adaptation solution (summary)

I searched online and found that many interviews ...

Introduction to MySQL <> and <=> operators

<> Operator Function: Indicates not equal t...

Solution to prevent caching in pages

Solution: Add the following code in <head>: ...

How to use map to allow multiple domain names to cross domains in Nginx

Common Nginx configuration allows cross-domain se...

Test and solution for MySQL's large memory usage and high CPU usage

After the changes: innodb_buffer_pool_size=576M -...

MySQL service and database management

Table of contents 1. Start and stop service instr...

Introduction to Docker containers

Docker Overview Docker is an open source software...

Linux file management command example analysis [display, view, statistics, etc.]

This article describes the Linux file management ...

MySQL count detailed explanation and function example code

Detailed explanation of mysql count The count fun...

Two ways to visualize ClickHouse data using Apache Superset

Apache Superset is a powerful BI tool that provid...

Causes and solutions for MySQL deadlock

The database, like the operating system, is a sha...

Detailed explanation of how to introduce custom fonts (font-face) in CSS

Why did I use this? It all started with the makin...

Native JS realizes uniform motion of various sports

This article shares with you a uniform motion imp...

Explore VMware ESXI CLI common commands

Table of contents 【Common commands】 [Summary of c...