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

How to use Celery and Docker to handle periodic tasks in Django

As you build and scale your Django applications, ...

Docker builds Redis5.0 and mounts data

Table of contents 1. Simple mounting of persisten...

Vue+js realizes video fade-in and fade-out effect

Vue+js realizes the fade in and fade out of the v...

MySQL date and time addition and subtraction sample code

Table of contents 1.MySQL adds or subtracts a tim...

Select does not support double click dbclick event

XML/HTML CodeCopy content to clipboard < div c...

How to implement image mapping with CSS

1. Introduction Image maps allow you to designate...

Use of Linux ln command

1. Command Introduction The ln command is used to...

Implementation of installing Docker in win10 environment

1. Enter the Docker official website First, go to...

MySQL 8.0.21.0 Community Edition Installation Tutorial (Detailed Illustrations)

1. Download MySQL Log in to the MySQL official we...

Implementation of importing and exporting docker images

Docker usage of gitlab gitlab docker Startup Comm...

In-depth explanation of slots and filters in Vue

Table of contents Slots What are slots? Slot Cont...

Detailed tutorial on compiling and installing MySQL 5.7.24 on CentOS7

Table of contents Install Dependencies Install bo...

Detailed explanation of overflow-scrolling to solve scrolling lag problem

Preface If you use the overflow: scroll attribute...