当前位置:网站首页>JS foundation 3
JS foundation 3
2022-06-28 10:53:00 【Programmer community】
Array
Definition : A collection that stores an ordered sequence of numbers
Array creation
- Constructors :
new Array()var arr=new array() - Literal
var arr=[]// An empty array
notes : Access array element index numbers - The array subscript ( Each array element corresponds to an index number , Array element index numbers are from 0 Start )
Array common methods
grammar : Array name . Method ()
| Array name | usage |
|---|---|
| push() | Array add elements |
| unshift() | Insert the first element |
| pop() | Delete the end of the element |
| shift() | Delete the front of the element |
| splice() | Delete any element of the array |
| sort() | Sort array |
| concat() | Array splicing |
| indexof() | Returns the numeric index number |
| findeIndex | Returns the numeric index number |
| join() | Link each item of the array , Become a string |
| includes() | Check whether the array contains the contents in parentheses |
| reverse | Inversion array |
notes :
splice(1,2,3) Indicates that there are three values , The first one means to delete the element index number , The second represents the deletion length , The third replacement element sort(function(a,b){return a-b}) Indicates normal sorting , That is, dictionary order sort(function(a,b){return b-a}) Indicates reverse sort
indexof() If the element does not exist , return -1
Sample code
<script> function scoreDuan() {
var numArr = [];
var scoreArray = [98, 78, 89, 77, 69, 59, 85, 100, 93, 81];
var a = 0,
b = 0,
c = 0,
d = 0,
e = 0,
f = 0,
g = 0,
h = 0,
j = 0,
k = 0,
m = 0;
for (let i = 0; i < scoreArray.length; i++) {
//scoreArray.length The length of the array if (scoreArray[i] == 100) {
a = a + 1
} else if (scoreArray[i] >= 90) {
b = b + 1
} else if (scoreArray[i] >= 80) {
c = c + 1
} else if (scoreArray[i] >= 70) {
d = d + 1
} else if (scoreArray[i] >= 60) {
e = e + 1
} else if (scoreArray[i] >= 50) {
f = f + 1
} else if (scoreArray[i] >= 40) {
g = g + 1
} else if (scoreArray[i] >= 30) {
h = h + 1
} else if (scoreArray[i] >= 20) {
j = j + 1
} else if (scoreArray[i] >= 10) {
k = k + 1
} else if (scoreArray[i] >= 0) {
m = m + 1
}
}
numArr.push(a, b, c, d, e, f, g, h, j, k, m)
console.log(numArr)
}
scoreDuan() // The output is 1,2,3,2,1,1,0,0,0,0,0 function delArr() {
var scoreArray = [89, 78, 69, 89, 90, 69, 81];
for (let i = 0; i < 7; i++) {
for (let k = i + 1; k < 7; k++) {
//k=i+1, Express K And i It's not equal , The array does not compare itself with itself if (scoreArray[i] == scoreArray[k]) {
scoreArray.splice(i, 1)
// If the arrays are equal, delete the array
}
}
}
console.log(scoreArray)
}
delArr() // The result is 89,78,69,90,81 function orderArr() {
var scoreArray = [98, 78, 86, 59, 94];
scoreArray.unshift(90);
// Add a... At the beginning 90
for (i = 0; i < scoreArray.length; i++) {
if (scoreArray[i] < 60) {
scoreArray.splice(i, 1)
// Delete less than 60 Number of numbers
}
}
console.log(scoreArray.sort())
// Output... In sequence
}
orderArr()
// The result is 78,86,90,94,98
</script>
Storage between data types
- Basic data variables are stored in the stack area
- Reference variables are stored in the stack area
- Complex data types are stored in the heap area

Traversal function
- for loop
var arr=[98,78,58,90,95,49]
for(i=0;i<arr.length;i++){
console.log(arr[i])
// result 98,78,58,90,95,49
}
- forEach: Traverse the output
var arr =[98,78,58,90,95,49]; arr.forEach(function(item,index,arr){
console.log(item,index)
})
// result 98 0
78 1
58 2
90 3
95 4
49 5
- map: Traverse the output
notes : And forEach The difference is that a new array is output - filter: Filter by output condition
function fun1_1(){
var arr =[98,78,58,90,95,49]; var newArr=arr.filter(function(item){
return item>=60;
// The judgment condition is greater than 60 Output
})
console.log(newArr)
}
fun1_1()
// result 98,78,90,95
- find: You can also filter by conditions , But only the first value that satisfies the condition is returned
function fun2_1(){
var arr =[98,78,58,90,95,49]; var newArr=arr.find(function(item){
return item<60;
})
console.log(newArr)
}
fun2_1()
// result 58
- some: As long as there is one in the array , return true
function fun5_1(){
var arr =[98,78,58,90,95,49]; let newArr=arr.some(function(item){
return item<60;
})
console.log(newArr)
}
fun5_1()
// The result is true
- every: All the elements in the array satisfy , return true
- reduce: Traversal array , Cumulative addition
var arr=[10,20]; var sum=arr.reduce(function(s,item,index){
//s To save the sum
return s+item
},0)//0 yes s Initial value
console.log(sum)
result :30
Bubble sort
If n Bubble sort by number , need n-1 Wheel bubbling , Make two bubbles each time , The former is bigger than the latter , Exchange order
// Bubble sort
var arr = [98, 86, 79, 69, 90, 82];
var n = arr.length;
for (let i = 0; i < n - 1; i++) {
//n-1 Secondary exchange
for (let j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
// The first number is greater than the second number
var stmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = stmp;
// Exchange two numbers
}
}
}
console.log(arr)
// result [69, 79, 82, 86, 90, 98]
Selection sort
If n Number to select and sort , need n-1 Secondary ranking , Let's assume that the first number is the smallest number , Compare him with the last number , Exchange the position between the lowest decimal and the first number
// Selection sort
var arr = [98, 86, 79, 69, 90, 82];
var n = arr.length;
for (i = 0; i < n-1; i++) {
let min = i;
// Definition No arr[i] The number is the minimum
for (j = 1 + i; j < n; j++) {
if (arr[j] < arr[min]) {
// If arr[j] It's the smallest number , Assign index numbers to min
min = j;
}
}
let stmp = arr[i];
arr[i] = arr[min];
arr[min] = stmp;
// The exchange index number is i and min Number of numbers
}
console.log(arr)
// result [69, 79, 82, 86, 90, 98]
边栏推荐
- 将浏览器中的文件 url转换为File流
- MySQL(三)
- Guangzhou Customs supports the stable supply of food, agricultural products, traditional Chinese medicine and other civilian and biological resources to Hong Kong
- Debug debugging in katalon
- 无线通信模块定点传输-点对多点的具体传输应用
- Yann Lecun's new paper: the road to building automatic agents
- OpenHarmony应用开发之二维码生成器
- 【功能建议】多个工作空间启动时选择某个空间
- Katalon framework tests web (XX) custom keywords and upload pop-up operations
- Interface automation framework scaffolding - Implementation of parametric tools
猜你喜欢

Remote connection of raspberry pie in VNC viewer mode without display

【力扣——动态规划】整理题目1:基础题目:509、70、746、62、63、343、96(附链接、题目描述、解题方法及代码)

fastposter v2.8.4 发布 电商海报生成器

港伦敦金行情走势图所隐藏的信息

Metersphere implements UI automation elements that are not clickable (partially occluded)

How does ETF position affect spot gold price?
![[monkey] Introduction to monkey test](/img/70/5a7152d0b6b77df7f9d6ad0e09e000.png)
[monkey] Introduction to monkey test

MySQL(一)

移动命令

Katalon当中的output使用方法
随机推荐
Ffmpeg audio and video recording
Oracle 日期格式化异常:无效数字
Katalon框架测试一个web页面操作实例代码
港伦敦金行情走势图所隐藏的信息
Training and recognition of handwritten digits through the lenet-5 network built by pytorch
Using loops for, while, and if in katalon else、break、continue
Does flink1.15 support MySQL views? I configured the view name at the table name to save, but the table could not be found. Think
JSON module, hashlib, Base64
GDB简介
An idea plug-in that automatically generates unit tests, which improves the development efficiency by more than 70%!
无线模块透明传输技术的物联网应用案例
Redis database
移动命令
fastposter v2.8.4 发布 电商海报生成器
Set up your own website (11)
乌国家安全与国防委员会秘书:将对俄境内目标进行精确打击
【Qt】connect 语法参考实现
Day 6 script and animation system
工控安全之勒索病毒篇
Mysql database overview and installation process