当前位置:网站首页>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++
}边栏推荐
- Oracle 组合查询
- LogCat工具
- tigervnc的使用
- Federal Reserve SR 11-7: Guidance on model risk management - Wanzi collection
- functools模块
- Which C4d cloud rendering platform to cooperate with?
- (2022杭电多校三)1009.Package Delivery(贪心)
- Introduction to network -- overview of VLAN and trunk
- 用shell来计算文本中的数字之和
- How MySQL executes query statements
猜你喜欢

C4D动画如何提交云渲染农场快速渲染?

【WSL2】配置连接 USB 设备并使用主机的 USB 摄像头

使用反射实现动态修改@Excel的注解属性

Esp8266 (esp-12f) third party library use -- sparkfun_ Apds9960 (gesture recognition)

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

单臂路由(讲解+实验)

The possibility of metauniverse from the perspective of technical principles: how Omniverse "built" Mars

sql-labs SQL注入平台-第1关Less-1 GET - Error based - Single quotes - String(基于错误的GET单引号字符型注入)

C# Winfrom 常用功能整合-2

(2022牛客多校三)A-Ancestor(LCA)
随机推荐
Excuse me, is there a big delay in individual capture when someone uses Oracle xStream? How to solve the delay problem
?实验 7 基于 Mysql 的 PHP 管理系统实现
12. Integer to Roman
Codeforces Round #809 (Div. 2)(6/6)(Kruskal重构树)
12. Integer to Roman整数转罗马数字
functools模块
(2022牛客多校三)A-Ancestor(LCA)
Oracle 组合查询
美联储SR 11-7:模型风险管理指南(Guidance on Model Risk Management)-万字收藏
(2022 Hangdian multi school III) 1011.taxi (Manhattan maximum + 2 points)
(2022 Niuke multi school III) a-ancestor (LCA)
oracle的触发器的使用举例
Drools (5): drools basic syntax (3)
Internal class -- just read this article~
一款开源 OA 办公自动化系统
word-wrap:break-word 换行在各浏览器兼容的问题
请问 mysql timestamp(6) 用flink-sql接过来是 null,这点有办法处理不
(2022杭电多校三)1011.Taxi(曼哈顿最值+二分)
使用sqlplus显示中文为乱码的解决办法
C# Winfrom 常用功能整合-2