js数组
for练习
<script>
//5行5列*
//行数
for (let index = 1; index <= 5 ; index++) {
//列数
for (let index2 = 1; index2 <= 5; index2++) {
//输出的文字
document.write('*')
}
//行数同级,输出换行
document.write('<br>')
}
</script>
<script>
//倒三角5行星星
for (let index = 1; index <=5; index++) {
for (let index1 = 1; index1 <= index; index1++) {
document.write('*')
}
document.write('<br>')
}
</script>
<script>
//99乘法表
for (let index = 1; index <= 9; index++) {
for (let index1 = 1; index1 <= index; index1++) {
let num = index1 * index;
document.write(`<span> ${index1} * ${index} = ${num} </span>`);
}
document.write('<br/>');
}
</script>
碰到 不会做的案例的时候 要怎么办
1 百度(最终的解决方案)
2 找和以前旧案例的联系!!! 尽可能去找!!!
(相等于每一个案例对你来是,都是全新,没有得到 每一个案例的开发经验!)
4 拆解
5 分析 主次功能
分析:
1 比较主要的是里面的文字 该是让显示
(要从行来开 不要从列来看 )
需求分析:(用文字 把看见的需求 描述下来)
数值
数组的基本语法
数组可以存储任意类型的数据
取值语法
通过下标取数值
取出来是什么类型的 就根据这种类型特点来访问
一些术语
元素 树主中保存的每个元素都叫数组
下标 数组中数据的编号
长度 数组中数据的个数 通过数组的length属性获得
for数组搭配使用
<script>
let arr = ["大喵","二喵","三喵","四喵","五喵"]
for (let index = 0; index < arr.length; index++) {
console.log(arr[index]);
}
</script>
操作数组
数组本质是数据集合, 操作数据无非就是 增 删 改 查 语法
查:询数组数据
数组[下标] 或者我们称为访问数组数据
改: 重新赋值
数组[下标] = 新值
增: 数组添加新的数据
数组结尾增加: arr.push(新增的内容)
<script>
let arr = ['苹果','香蕉','橙子']
arr.push('西瓜')
console.log(arr);
</script>
数组开头增加: arr.unshift(新增的内容)
删:除数组中数据
删除数组结尾 arr.pop()
pop去掉最后一个,并返回最后一个名称
<script>
let arr = ['苹果','香蕉','橙子']
//pop去掉最后一个,并返回最后一个名称
let last = arr.pop()
console.log(last);
console.log(arr);
</script>
删除数组开头arr.shift()
first去掉第一个,并返回第一个名称
<script>
let arr = ['苹果','香蕉','橙子']
//first去掉第一个,并返回第一个名称
let first = arr.shift()
console.log(first);
console.log(arr);
</script>
删除任意数组位置的名称 arr.splice(操作的下标,删除的个数)
<script>
let arr = ['苹果','香蕉','橙子']
//指定位置删除数组,第一个数写位置,第二个数写删除的个数,可以返回删除的数据
let last = arr.splice(1,2)
console.log(last);
console.log(arr);
</script>
复制代码
<script>
let arr = ['苹果','香蕉','橙子']
//第二个位置写0,可以不删除,然后写内容,在指定位置添加数组内容
let last = arr.splice(1,0,'梨')
console.log(last);
console.log(arr);
</script>
数组补充
.length数组长度