当前位置:网站首页>Array method and loop in JS

Array method and loop in JS

2022-07-27 07:25:00 Ape code takes the lead

Catalog

One 、 Array

1. Array creation

2. Two dimensional array

3. Array common methods and properties

(1)

(2)

(3) Array loop

Two 、 loop  

1.for loop

2.while loop


One 、 Array

Array : A collection of one or more values with the same data type

1. Array creation

let Array name =new Array(size);  size: Represents the number of elements that can be stored in the array

let Array name = ["","",""]

Array subscript from 0 Start ..

2. Two dimensional array

let arr = [[],"","",[]];

There is an array inside the array

The three dimensional , The four are similar

3. Array common methods and properties

(1)

Category name explain
attribute length Sets or returns the number of elements in an array
Method join() Put all the elements of the array into a string , Separated by a separator
sort() Sort the array
push() Add one or more elements to the end of the array , And returns the new length

notes :join() Method examples

     let arr = [[12,15,13]," Zhang San "," Zhao si ","wang",["miau","dawod","dakonm"]];
     let str =  arr.join("@");
     console.log(str);

  The array will become a string

sort() Method examples   

If used directly sort() Method

  Pictured , Only the first digit will be sorted

Realize positive order , The reverse :

 arr.sort(function(a,b){
        return a-b;// positive sequence 
    });
    console.log(arr);
    arr.sort(function(a,b){
        return b-a;// The reverse 
    });
    console.log(arr);

 

(2)

                         flat(): Reduce the dimension of the array

push() Add an element to the end of the array
pop() Delete array end element
unshift() Add elements to the array header
shift() Delete the array header element
splice() Delete the elements in the array ( The first parameter is the starting index , second Is the number of deletes )

notes :splice() Method can also replace elements splice( Starting index , Number of replacements , Replacement value )

(3) Array loop

for of loop  

eg:for(let value of arr)

Loop through the value of each subscript in the array

for in loop

eg:for(let index in arr)

Loop through the subscript of the array

forEach() loop

eg:forEach(function(value,index,oldarr){})

Three parameters value Represents the values in an array ,index Represents array subscript ,oldarr Represents the array before the loop

map() Array methods :

There is a return value , It will return a new array with the same length as the old array .

 let newarr = arr.map(function(value,index,oldarr){return value;});

flatMap() Array methods

There is a return value , Return a new array with the same length as the old array , The new array will reduce the dimension of the old array ( Two to one , Three dimensional to two dimensional )

 let newarr = arr.flatMap(function(value,index,oldarr){return value;});

Two 、 loop  

1.for loop

for ( The initial part ; The loop condition ; Conditional iteration ) {

      // Statements executed in a loop

}

example : Loop through groups

    let arr = ["html","css","js","vue","wechat"]
    for (let i = 0; i < arr.length; i++) {
       document.write(arr[i]+"<br/>"); 
    }

2.while loop

while( The loop condition ) {

     // Cyclic operation

     // Iteration part

}

    let arr = ["html","css","js","vue","wechat"]
    let j = 0;
    while(j<arr.length){
        document.write(arr[j]+"<br/>");
        j++
    }

原网站

版权声明
本文为[Ape code takes the lead]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/208/202207270610254176.html