当前位置:网站首页>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]
边栏推荐
- Katalon当中的output使用方法
- 物联网无线通信应用中6种融合定位技术
- Mongo数据库
- MySQL(二)
- 如何利用k线图做技术分析
- Guangzhou Customs supports the stable supply of food, agricultural products, traditional Chinese medicine and other civilian and biological resources to Hong Kong
- Katalon global variable is referenced in testobject
- 各位大佬,问下Mysql不支持EARLIEST_OFFSET模式吗?Unsupported star
- Remote connection of raspberry pie in VNC viewer mode without display
- 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
猜你喜欢

Katalon当中的debug调试

etf持仓如何影响现货金价?

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

Summary of characteristics of five wireless transmission protocols of Internet of things

sentinel

Idea failed to connect to SQL Sever

第六天 脚本与动画系统
![[unity][ecs] learning notes (II)](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[unity][ecs] learning notes (II)

Sqlcmd database connection error

Markdown -- basic usage syntax
随机推荐
Hystrix deployment
广州海关支持保障食品、农产品和中药材等民生物资稳定供港
Google open source dependency injection framework Guice Guide
How to use output in katalon
Metersphere implements UI automation elements that are not clickable (partially occluded)
The boss asked me to write an app automation -- yaml file reading -- with the whole framework source code attached
[Li Kou - dynamic planning] sort out topic 1: basic topics: 509, 70, 746, 62, 63, 343, 96 (with links, topic descriptions, problem solving methods and codes)
Who knows if it is safe to open an account with CSC securities
Metersphere实现UI自动化元素不可点击(部分遮挡)
【agora】get 一个 agora_refptr 对象的用法示例
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
MySQL(三)
Teach you how to handle the reverse SVG mapping of JS
JSON module, hashlib, Base64
Pop up and push in sequence of stack < difficulty coefficient >
BLE蓝牙模块NRF518/NRF281/NRF528/NRF284芯片方案对比
Redis数据库
乌国家安全与国防委员会秘书:将对俄境内目标进行精确打击
Spatial-Temporal时间序列预测建模方法汇总
June training (day 28) - Dynamic Planning