JavaScript basics for loop and array

JavaScript basics for loop and array

Loop - for

Basic use of for loop

for loop syntax : repeatedly execute code

Advantages : Write the declaration starting value, loop condition, and change value together, making it clear at a glance

   for (variable starting value; loop condition; variable update) {
     Loop Body}

The difference between for loop and while loop:

  • It is recommended to use a for loop when the number of loops is clear.
  • When the number of loops is unclear, it is recommended to use a while loop

Exit the loop

End of loop:

  • continue : End this loop and continue to the next loop
  • break : jump out of the current loop

Nested loops

A loop inside another loop, usually used in a for loop

    for (variable starting value; loop condition; variable update) {
      for (variable starting value; loop condition; variable update) {
        Loop Body}
    }

Arrays

What is an array?

Array is a data type that can store data in sequence.

Basic use of arrays

Declaration Syntax

let array name = [data1, data2, ..., datan]

1The array is saved in order, so each data has its own number

2. Numbering in computers starts at 0, and so on

3. In an array, the number of data is also called index or subscript

4. Arrays can store any type of data

Value Syntax

Array name[subscript]

1. Get data by subscript

2. What type of data is retrieved? Then access it based on the characteristics of that type.

Some terminology

  • Element: Each data stored in an array is called an array element
  • Subscript: the number of the data in the array
  • Length: The number of data in the array, obtained through the length attribute of the array
Array name.length

Iterating over an array

Use a loop to access each element in the array, usually using a for loop

    for (let i = 0; i < array name.length; i++) {
      Array name[i]
    }

Manipulating Arrays

The essence of an array is a collection of data. To operate data is nothing more than adding, deleting, modifying and checking syntax.

1. Check: query array data, or we call it access array data array [subscript]

2. Change: Reassign array [subscript] = new value

3. Increase: add new data to the array

  • arr.push (new content)
  • arr.unshift (new content)

4. Delete: Delete the data in the array

  • arr.pop()
  • arr.shift()
  • arr.splice (operation subscript, number of deletions)

Add new data to the array

數組.push() method adds one or more elements to the end of an array and returns the new length of the array (important)

arr.push(element 1, element 2, ..., element n)

arr.unshift (new) method adds one or more elements to the beginning of an array and returns the new length of the array

arr.unshift(element1, element2, ..., elementn)

Deleting elements from an array

數組. pop() method removes the last element from the array and returns the value of the element

arr.pop()

Array.shift() method removes the first element from an array and returns the value of that element.

arr.shift()

Array. splice() method deletes the specified element (key point)

arr.splice(start, deleteCount)
arr.splice (starting position, delete several elements)

start Starting position:

Specify the start position of the modification (counting from 0) deleteCount :

Indicates the number of array elements to be removed

Optional. If omitted, the default is to delete from the specified starting position to the end

Deleting elements:

Random draw, the winning user needs to be deleted from the array, and duplicate draws are not allowed

Click the Delete button and the related data will be deleted from the product data

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
  • A brief introduction to JavaScript arrays
  • Commonly used JavaScript array methods

<<:  Docker overlay realizes container intercommunication across hosts

>>:  MySQL sharding details

Recommend

A brief analysis of the differences between undo, redo and binlog in MySQL

Table of contents Preface 【undo log】 【redo log】 【...

How to remount the data disk after initializing the system disk in Linux

Remount the data disk after initializing the syst...

JS implements circular progress bar drag and slide

This article example shares the specific code of ...

MySQL replication mechanism principle explanation

Background Replication is a complete copy of data...

Detailed explanation of monitoring Jenkins process based on zabbix

1. Monitoring architecture diagram 2. Implementat...

Dockerfile text file usage example analysis

Dockerfile is a text file used to build an image....

Vue project realizes login and registration effect

This article example shares the specific code of ...

Detailed explanation of how to migrate a MySQL database to another machine

1. First find the Data file on the migration serv...

VUE+SpringBoot implements paging function

This article mainly introduces how to implement a...

Perfect solution to Docker Alpine image time zone problem

Recently, when I was using Docker to deploy a Jav...

Detailed explanation of the correct way to install opencv on ubuntu

This article describes how to install opencv with...

How to understand JavaScript modularity

Table of contents 1. Browser support 2. export ex...

How to set utf-8 encoding in mysql database

Modify /etc/my.cnf or /etc/mysql/my.cnf file [cli...

Implementation of importing and exporting vue-element-admin projects

vue-element-admin import component encapsulation ...

Implementation of CSS heart-shaped loading animation source code

Without further ado, let me show you the code. Th...