当前位置:网站首页>JS基础3
JS基础3
2022-06-28 10:49:00 【程序员社区】
数组
定义:存储一系列有序数列的集合
数组创建
- 构造函数:
new Array()var arr=new array() - 字面量
var arr=[]//空数组
注:访问数组元素索引号-数组下标(每个数组元素对应一个索引号,数组元素索引号从0开始)
数组常用方法
语法:数组名.方法()
| 数组名 | 用法 |
|---|---|
| push() | 数组添加元素 |
| unshift() | 最前面插入元素 |
| pop() | 删除元素最末尾 |
| shift() | 删除元素最前面 |
| splice() | 删除数组任意元素 |
| sort() | 排序数组 |
| concat() | 数组拼接 |
| indexof() | 返回数字索引号 |
| findeIndex | 返回数字索引号 |
| join() | 把数组每一项链接起来,成为字符串 |
| includes() | 检测数组里是否包含括号中内容 |
| reverse | 反转数组 |
注:
splice(1,2,3)表示有三个值,第一个表示删除元素索引号,第二个表示删除长度,第三个替换元素sort(function(a,b){return a-b})表示正常排序,也就是字典顺序sort(function(a,b){return b-a})表示反向排序
indexof()如果元素不存在,返回-1
示例代码
<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数组长度 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() //输出结果为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,表示K与i不相等,数组不自己与自己比较 if (scoreArray[i] == scoreArray[k]) {
scoreArray.splice(i, 1)
//如果数组相等删除数组
}
}
}
console.log(scoreArray)
}
delArr() //结果为89,78,69,90,81 function orderArr() {
var scoreArray = [98, 78, 86, 59, 94];
scoreArray.unshift(90);
//最前面添加一个90
for (i = 0; i < scoreArray.length; i++) {
if (scoreArray[i] < 60) {
scoreArray.splice(i, 1)
//删除小于60的数
}
}
console.log(scoreArray.sort())
//按顺序输出
}
orderArr()
//结果为78,86,90,94,98
</script>
数据类型之间存储
- 基本数据变量存储在栈区域
- 引用变量存储在栈区域
- 复杂数据类型存储在堆区域

遍历函数
- for循环
var arr=[98,78,58,90,95,49]
for(i=0;i<arr.length;i++){
console.log(arr[i])
//结果98,78,58,90,95,49
}
- forEach:遍历输出
var arr =[98,78,58,90,95,49]; arr.forEach(function(item,index,arr){
console.log(item,index)
})
//结果 98 0
78 1
58 2
90 3
95 4
49 5
- map:遍历输出
注:与forEach区别在于输出一个新的数组 - filter:按条件过滤输出
function fun1_1(){
var arr =[98,78,58,90,95,49]; var newArr=arr.filter(function(item){
return item>=60;
//判定条件大于60输出
})
console.log(newArr)
}
fun1_1()
//结果98,78,90,95
- find:也可按条件过滤,但只返回第一个满足条件的值
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()
//结果58
- some:数组中只要有一个,返回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()
//结果为true
- every:数组中所有元素满足,返回true
- reduce:遍历数组,累计求加
var arr=[10,20]; var sum=arr.reduce(function(s,item,index){
//s用来保存求和
return s+item
},0)//0是s初始值
console.log(sum)
结果:30
冒泡排序
如果n个数进行冒泡排序,需要n-1轮冒泡,每次冒泡两两进行,前一个比后一个大,交换顺序
//冒泡排序
var arr = [98, 86, 79, 69, 90, 82];
var n = arr.length;
for (let i = 0; i < n - 1; i++) {
//n-1次交换
for (let j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
//第一个数大于第二个数
var stmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = stmp;
//交换两个数
}
}
}
console.log(arr)
//结果[69, 79, 82, 86, 90, 98]
选择排序
如果n个数进行选择排序,需要n-1次排序,先假设第一个数为最小数,用后面数和他比较,最小数和第一个数交换位置
//选择排序
var arr = [98, 86, 79, 69, 90, 82];
var n = arr.length;
for (i = 0; i < n-1; i++) {
let min = i;
//定义第arr[i]个数为最小数
for (j = 1 + i; j < n; j++) {
if (arr[j] < arr[min]) {
//如果arr[j]是最小数,赋值索引号给min
min = j;
}
}
let stmp = arr[i];
arr[i] = arr[min];
arr[min] = stmp;
//交换索引号为i和min的数
}
console.log(arr)
//结果[69, 79, 82, 86, 90, 98]
边栏推荐
猜你喜欢
![[practice] appium settings app is not running after 5000ms](/img/3c/5a7544561231a15a4bb5cfa9e102b3.png)
[practice] appium settings app is not running after 5000ms
![[Unity][ECS]学习笔记(二)](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[Unity][ECS]学习笔记(二)

Realize an air conditioner with compose to bring you cool in summer

How to use K-line diagram for technical analysis

BLE蓝牙模块NRF518/NRF281/NRF528/NRF284芯片方案对比

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

MySQL cannot be opened. Flash back
![[200 opencv routines] 213 Draw circle](/img/8d/a771ea7008f84ae3a3cf41507448ec.png)
[200 opencv routines] 213 Draw circle

Internet of things application case of wireless module transparent transmission technology

ruoyi集成积木报表(nice)
随机推荐
Generate token
[leetcode daily question] [December 19, 2021] 997 Find the town judge
sentinel
[unity] built in rendering pipeline to URP
JSON module, hashlib, Base64
MySQL (II)
Katalon当中的output使用方法
【Qt】connect 语法参考实现
fastposter v2.8.4 发布 电商海报生成器
如何使用 DataAnt 监控 Apache APISIX
Resolution: overview of decentralized hosting solution
Interface automation framework scaffold - use reflection mechanism to realize the unified initiator of the interface
MySQL common commands for viewing database performance
Installing MySQL database (CentOS) in Linux source code
Spatial-Temporal时间序列预测建模方法汇总
一种跳板机的实现思路
BLE蓝牙模块NRF518/NRF281/NRF528/NRF284芯片方案对比
Katalon当中的使用循环for、while和if...else、break、continue
Metersphere implements UI automation elements that are not clickable (partially occluded)
[Unity]EBUSY: resource busy or locked