JavaScript数组常用操作
javascript 数组常用操作
1、读取数组数据 for循环 或者for in循环
let data =[2000,1000,"aaa","bbb","ccc","abc",1000];
for(index in data){
console.log("for-in输出数据"+index+":"+data[index]);
}
for(let i=0;i
2、合并数组concat()
let data2a =[2000,1000,"aaa","bbb","ccc","abc",1000];
let data2b=["中国人","aabb",200]
var newdata2=data2a.concat(data2b);
console.log("合并后数组:"+newdata2);
3、数组变为字符串格式 join()
let data3 =[2000,1000,"aaa","bbb","ccc","abc",1000];
var newdata3=data3.join();
console.log("数组转换成字符串:"+newdata3);
4、删除数组第一个元素shift()
let data4 =["删除第一个",1000,"aaa","bbb","ccc","abc",1000];
let newdata4=data4.shift();
console.log("返回删除的元素:"+newdata4);
console.log("删除后数组:"+data4);
5、删除数组最后一个个元素pop()
let data5 =[2000,1000,"aaa","bbb","ccc","abc",1000,"删除最后一个"];
let newdata5=data5.pop();
console.log("返回删除的元素:"+newdata5);
console.log("删除后数组:"+data5);
6、数组开头添加一个元素unshift()
let data6=[2000,1000,"aaa","bbb","ccc","abc",1000];
var newdata6=data6.unshift("开头添加一个元素");
console.log("返回添加后数组长度:"+newdata6);
console.log("添加后数组:"+data6);
7、数组末尾添加一个元素 push()
let data7=[2000,1000,"aaa","bbb","ccc","abc",1000];
var newdata7=data7.push("末尾添加一个元素");
console.log("返回添加后数组长度:"+newdata7);
console.log("添加后数组:"+data7);
8、反转数组reverse()
let data8=["第一",2000,1000,"aaa","bbb","ccc","abc","倒数第一"];
let newdata8=data8.reverse();
console.log("反转后数组:"+newdata8);
9、数组排序 sort() a-b 升序 b-a 降序
let data9a=[-20,0,12,23,-200,100,300,-15];
let data9b=[-20,0,12,23,-200,100,300,-15];
let newdata9a=data9a.sort(function(a,b){return b-a});
let newdata9b=data9b.sort(function(a,b){return a-b});
console.log("降序:"+newdata9a);
console.log("升序:"+newdata9b);
10、slice截取数组 slice(start,end) 下标
let data10=["你好","aa","bbbb",200,"中国人"];
let newdata10a=data10.slice(2);
let newdata10b=data10.slice(-5);
let newdata10c=data10.slice(2,4);
let newdata10d=data10.slice(-5,-3);
console.log("原数组:"+data10);
console.log("1、只有起起始值,并且为正,默认的从起始位置一直到整个数组结束,起始下标从0开始:"+newdata10a);
console.log("2、只有起起始值,并且为负,默认的从起始位置一直到整个数组结束,数组的最后一个下标是-1:"+newdata10b);
console.log("3、有起始值和结束值,并且为正,截取的下标个数并不包含最后一个下标:"+newdata10c);
console.log("4、有起始值和结束值,并且为负:"+newdata10d);
11、数组删除、替换、添加 splice(index)
let data11a=["aaa","bbb","cccc",200,"二丫"]
let data11b=["aaa","bbb","cccc",200,"二丫"]
let data11c=["aaa","bbb","cccc",200,"二丫"]
let data11d=["aaa","bbb","cccc",200,"二丫"]
let newdata11a=data11a.splice(2)
let newdata11b=data11b.splice(2,1)
let newdata11c=data11c.splice(2,1,"替换元素")
data11d.splice(2,0,"新添加的元素")
console.log("原数组:"+data11a);
console.log("删除指定下标后面所有元素:"+newdata11a);
console.log("原数组:"+data11b);
console.log("删除指定下标开始,指定个数:"+newdata11b);
console.log("原数组:"+data11c);
console.log("指定个数替换元素:"+newdata11c);
console.log("原数组:"+data11d);
12、更改数组指定值
let data12=["你好","女",333,"bb","中国人"]
for(index in data12){
if(data12[index]=="中国人"){
data12[index]="小日本";
}
}
console.log("替换指定值:"+data12);
输出结果:
for-in输出数据0:2000
for-in输出数据1:1000
for-in输出数据2:aaa
for-in输出数据3:bbb
for-in输出数据4:ccc
for-in输出数据5:abc
for-in输出数据6:1000
for 输出数据:0:2000
for 输出数据:1:1000
for 输出数据:2:aaa
for 输出数据:3:bbb
for 输出数据:4:ccc
for 输出数据:5:abc
for 输出数据:6:1000
合并后数组:2000,1000,aaa,bbb,ccc,abc,1000,中国人,aabb,200
数组转换成字符串:2000,1000,aaa,bbb,ccc,abc,1000
返回删除的元素:删除第一个
删除后数组:1000,aaa,bbb,ccc,abc,1000
返回删除的元素:删除最后一个
删除后数组:2000,1000,aaa,bbb,ccc,abc,1000
返回添加后数组长度:8
添加后数组:开头添加一个元素,2000,1000,aaa,bbb,ccc,abc,1000
返回添加后数组长度:8
添加后数组:2000,1000,aaa,bbb,ccc,abc,1000,末尾添加一个元素
反转后数组:倒数第一,abc,ccc,bbb,aaa,1000,2000,第一
降序:300,100,23,12,0,-15,-20,-200
升序:-200,-20,-15,0,12,23,100,300
原数组:你好,aa,bbbb,200,中国人
1、只有起起始值,并且为正,默认的从起始位置一直到整个数组结束,起始下标从0开始:bbbb,200,中国人
2、只有起起始值,并且为负,默认的从起始位置一直到整个数组结束,数组的最后一个下标是-1:你好,aa,bbbb,200,中国人
3、有起始值和结束值,并且为正,截取的下标个数并不包含最后一个下标:bbbb,200
4、有起始值和结束值,并且为负:你好,aa
原数组:aaa,bbb
删除指定下标后面所有元素:cccc,200,二丫
原数组:aaa,bbb,200,二丫
删除指定下标开始,指定个数:cccc
原数组:aaa,bbb,替换元素,200,二丫
指定个数替换元素:cccc
原数组:aaa,bbb,新添加的元素,cccc,200,二丫
替换指定值:你好,女,333,bb,小日本