当前位置:网站首页>Array method and loop in JS
Array method and loop in JS
2022-07-27 07:25:00 【Ape code takes the lead】
Catalog
3. Array common methods and properties
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++
}边栏推荐
- VLAN trunk experiment
- MySQL query operation index optimization practice
- 用shell来计算文本中的数字之和
- 在rhel8上使用soci连接oracle和postgresql和sqlite
- 在kettle中快速更新一个字段中的信息
- 网络入门——vlan及trunk概述
- DRConv-pytorch改称输出和输入一样的尺寸
- 腾讯云服务器SSH链接自动断开解决方法
- Li Mu hands-on learning, in-depth learning, V2 transformer and code implementation
- C语言程序设计 | 程序编译与预处理
猜你喜欢

美联储SR 11-7:模型风险管理指南(Guidance on Model Risk Management)-万字收藏

Pan Aimin, chairman of instruction set, attended the 2022 ecug con to speak for China's technical forces

MySQL2

(2022牛客多校三)J-Journey(dijkstra)

C4D云渲染平台选哪家合作?

docker安装MySQL8.0.28

零号培训平台课程-1、SQL注入基础

(2022 Hangdian multi school III) 1009.package delivery (greedy)

Pytorch notes: td3

The possibility of metauniverse from the perspective of technical principles: how Omniverse "built" Mars
随机推荐
2022-07-25 顾宇佳 学习笔记
ShowDoc漏洞学习——CNVD-2020-26585(任意文件上传)
在kettle中快速更新一个字段中的信息
Oracle数据库问题
How to implement Devops with automation tools | including low code and Devops application practice
(2022牛客多校三)J-Journey(dijkstra)
How MySQL executes query statements
How to submit C4d animation to cloud rendering farm for fast rendering?
使用反射实现动态修改@Excel的注解属性
(2022杭电多校三)1011.Taxi(曼哈顿最值+二分)
Internal class -- just read this article~
SQLite 常用功能整合
零号培训平台课程-2、SSRF基础
Please ask the big guys a question. The pgsqlcdc task can't monitor changes after running for a period of time. Just restart it. What should I do
2022 0726 顾宇佳 学习笔记
MySQL index failure and solution practice
Convert Excel to csv/csv UTF-8
Confluence漏洞学习——CVE-2021-26084/85,CVE-2022-26134漏洞复现
在kettle使用循环来处理表中的数据
Shell系统学习之Shell条件测试,判断语句和运算符