JS_数组

数组的定义:一个标识符,可以存储多个数据,并且数据的类型可以不同。
一、创建数组的方法:
1、字面量方式
书写格式:关键字+标识符+赋值符号+中括号+分号
let arr = [1,2,3,4];
中括号里面每个数据之间的分隔符是逗号;每个数据成为元素,每个元素都有自己在数组中的唯一引索(下标),我们可以通过引索(下标)来获取数据,下标的起始值是0,访问数组一个不存在的下标,会得到undefined
例:
console.log(account1[100]);//输出:undefined
let account1=[“f72”,“123”,1000,false]
let account2=[“f73”,“456”,2000,true]
console.log(account1,account2);//输出:[ ‘f72’, ‘123’, 1000, false ] [ ‘f73’, ‘456’, 2000, true ]
console.log(account1[0]);//输出:f72
console.log(account1[1]);//输出:123
2、构造函数
书写格式:关键字+标识符+赋值符号+new Array()+分号
let Array = new Array();
二、数组的属性和方法:
获取数组的长度(数据个数):数组名.length
数组最后一个值的下标 arr.length - 1
新增:arr[arr.length]=新增内容;
1、新增
书写格式1:数组名.push(添加的内容)
注:添加内容可以是多个数据(分隔符为逗号)
功能:在数组后面添加数据
例:
f72.push(true,false);
console.log(f72);//[ ‘a’, ‘b’, ‘c’, true, false ]
书写格式2:数组名.unshift(添加的内容)
注:添加内容可以是多个数据(分隔符为逗号)
功能:在数组前面添加数据
例:
f72.unshift(null,undefined);
console.log(f72);//[ null, undefined, ‘a’, ‘b’, ‘c’, true, false ]
2、删除
书写格式1:数字名.pop()
功能:删除数组最后一个数据(小括号里面添加内容无效)
例:
f72.pop();
console.log(f72);//[ ‘a’, ‘b’ ]
书写格式2:数字名.shift()
功能:删除数组第一个数据(小括号里面添加内容无效)
例:
f72.shift();
console.log(f72);//[ ‘b’, ‘c’ ]
三、【slice()方法】
1、不会改变原数据
2、返回参数对应的数据
1个参数表示获取数据的起始下标,从这个下标开始一直获取到最后一个
2个参数,第一个表示获取数据的起始下标,第二个表示获取数据的结束下标但是不包括本身
例:
let f72 = [“a”, “b”, “c”, “d”, “e”, “f”];
let f71=f72.slice(1,3);
console.log(f72);//[ ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’ ]
console.log(f71);//[ ‘b’, ‘c’ ]
let f73 = f72.slice(-2, -1);
let f74 = f72.slice(-5, -3);
console.log(f73);//[ ‘e’ ]
console.log(f74);//[ ‘b’, ‘c’ ]
四、splice方法(删除、修改、插入)
删除:一个参数表示其实下标一直到最后。两个参数第一个表示起始下标,第二个表示获取个数
例:
let f72 = [“a”, “b”, “c”, “d”, “e”, “f”];
let f73 = f72.splice(3);
console.log(f72);//[ ‘a’, ‘b’, ‘c’ ]
console.log(f73);//[ ‘d’, ‘e’, ‘f’ ]

let f74=f72.splice(1,2);
console.log(f72);//[ ‘a’, ‘d’, ‘e’, ‘f’ ]
console.log(f74);//[ ‘b’, ‘c’ ]

let f75=f72.splice(3,1);
console.log(f72);//[ ‘a’, ‘b’, ‘c’, ‘e’, ‘f’ ]
console.log(f75);//[ ‘d’ ]
修改:
三个参数:参数1:起始下标; 参数2:个数(修改几个数据),参数3:修改后的数据
多个参数:参数1:起始下标; 参数2:个数(修改几个数据);参数3及以后的参数:修改后的数据
例:
let f72 = [“a”, “b”, “c”, “d”, “e”, “f”];
let f73=f72.splice(1,1,1);
console.log(f72);//[ ‘a’, 1, ‘c’, ‘d’, ‘e’, ‘f’ ]

let f74=f72.splice(1,2,1)
console.log(f72);//[ ‘a’, 1, ‘d’, ‘e’, ‘f’ ]
console.log(f74);//[ ‘b’, ‘c’ ]

let f75=f72.splice(1,1,1,“true”,“false”)
console.log(f72);//[ ‘a’, 1, ‘true’, ‘false’, ‘c’, ‘d’, ‘e’, ‘f’ ]
console.log(f75);//[ ‘b’ ]
插入:
三个参数:参数1:起始下标;参数2:0;参数3:插入参数1前的数据
多个参数:参数1:起始下标;参数2:0;参数3及以后的参数:插入参数1前的数据
例:
let f72 = [“a”, “b”, “c”, “d”, “e”, “f”];
let f73=f72.splice(1,0,1);
console.log(f72);//[‘a’, 1, ‘b’,‘c’, ‘d’, ‘e’,‘f’]
console.log(f73);//[]
数组去重:
let f72 = [1, 2, 3, 1, 4, 1, 5, 5, 5, 1, 6, 7, 1, 4, 9, 0];
for (let i = 0; i < f72.length; i++) {
if (f72.indexOf(f72[i]) != f72.lastIndexOf(f72[i])) {
f72.splice(f72.lastIndexOf(f72[i]), 1);
i–;
}
}
console.log(f72);//[1, 2, 3, 4, 5, 6, 7, 9, 0]
for of 遍历数组中的每个值
for(let items of arr){
console.log(items);
}
for in 遍历数组中的每个下标
for (let index in arr){
console.log(index,arr[index]);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值