1:创建数组
var arr1
= [1,2,3];
console
.log(arr1
);
var arr2
= new Array(4,5,6);
console
.log(arr2
);
var arr3
= Array(7,8,9);
console
.log(arr3
);
var arr4
= Array
.of(10,11,12);
console
.log(arr4
);
var arrSingle
= Array(6);
console
.log(arrSingle
);
var arrSingle2
=new Array(3);
console
.log(arrSingle2
);
var arrSingle3
= Array
.of(10);
console
.log(arrSingle3
);
var arrSingle4
= [9];
console
.log(arrSingle4
);
2:访问数组(通过下标或者索引实现)
var arr
= [1,2,3];
console
.log(arr
.length
);
console
.log(arr
[0]);
console
.log(arr
[1]);
console
.log(arr
[2]);
console
.log(arr
[5]);
3:数组添加元素
var arr
= [1,2,3];
arr
[0] = 4;
console
.log(arr
);
arr
[3] = 5;
console
.log(arr
);
arr
[7] = 8;
console
.log(arr
);
console
.log(arr
[6]);
var arr2
= [1,2,3,4]
arr2
.splice(2,0,7,8,9);
console
.log(arr2
);
4:数组删除元素
var arr
= [1,2,3];
arr
.length
= 2;
console
.log(arr
);
arr
.length
= 0;
console
.log(arr
);
var arr2
= [1,2,3,4,5,6]
arr
.splice(2,1);
console
.log(arr2
);
arr
.splice(1,2,3,7,8);
console
.log(arr2
);
5:数组遍历
var arr
= [1,3,5,7,9];
for(let i
;i
<arr
.length
;i
++){
console
.log(arr
[i
]);
}
for(let ele
of arr
){
console
.log(ele
);
}
arr
.forEach((ele
,index
,self
)=>{
console
.log(ele
,index
,self
);
});
6:栈模式(栈是一种后进先出的数据结构)push(),pop()
var stack
= [1,2,3];
stack
.push(4);
console
.log(stack
);
stack
.push(5,6,7);
console
.log(stack
);
var last
= stack
.pop();
console
.log(last
);
console
.log(stack
);
console
.log(stack
[stack
.length
-1]);
7:队列模式(队列是一种后进先出的数据结构)push()、shift(),unshift()插队
var queue
= [1,2,3];
queue
.push(4,5,6);
console
.log(queue
);
var first
= queue
.shift();
console
.log(first
);
console
.log(queue
);
queue
.unshift(10,11,12);
console
.log(queue
);
8:反转数组(reverse()方法)
var arr
= [1,2,3];
console
.log(arr
.reverse());
console
.log(arr
);
var a
= "hello".split("");
console
.log(a
);
var b
= a
.reverse();
console
.log(b
);
var c
= b
.join("");
console
.log(c
);
9:数组排序(sort()方法)
var arr
= [5,3,2,7,6];
arr
.sort();
console
.log(arr
);
arr
.sort(a
,b
)=>{
if(a
> b
){
return 1;
}else if(a
< b
){
return 1;
}else{
return 0;
}
}
console
.log(arr
);
10:数组连接concat()
var arr1
= [1,2,3];
var arr2
= [4,5,6];
console
.log(arr1
.concat(arr2
));
11:数组裁切slice()
var arr
= [1,2,3,4];
console
.log(arr
.slice(1));
console
.log(arr
.slice(1,3));
console
.log(arr
.slice(1,-1));
12:数组map(map会对每个元素进行操作后,返回一个新的数组,接收三个参数(当前元素,当前索引,数组本身)
var arr
= [1,2,3];
var mappedArr
= arr
.map(ele
=> ele
*2);
console
.log(mappedArr
);
console
.log(arr
);
13:数组reduce,接收四个参数(上一次计算的结果,当前遍历到的数组元素,当前遍历到的索引,数组本身)
var arr
= [1,2,3,4];
var result
= arr
.reduce((previous
,current
) => previoue
+ current
,0);
console
.log(result
);
var result2
= arr
.reduce((first
,second
) => first
+second
);
console
.log(result2
);
14:数组过滤filter(接收的参数(当前遍历的元素,当前索引,数组本身))
var arr
= [1,2,3,4,5,6];
var filteredArr
= arr
.filter(item
=> item
>4);
console
.log(filteredArr
);
15:数组测试
存在两种
1)检测所有元素是否满足一定条件,满足的话,返回true,不满足返回false
2)如果有一个元素满足条件,返回true,否则的花返回false
var arr
= [1,2,3,4];
var result
= arr
.every(item
=> item
>0);
console
.log(result
);
var result2
= arr
.every(item
=> item
>2);
console
.log(result2
);
var resultsome
= arr
.some(item
=> item
>3);
console
.log(resultsome
);
var resultsome2
= arr
.some(item
=> item
>4);
console
.log(resultsome2
);
16:destructuring解构操作符(把数组的元素赋值给变量)
var arr
= [1,2,3];
var [a
,b
,c
] = arr
;
console
.log(a
,b
,c
);
var [d
,e
] = arr
;
console
.log(d
,e
);
var [,f
] = arr
;
console
.log(f
);
function np(){
let name
= "z";
let age
= 19;
return np
[name
,age
];
}
var [myName
,myAge
] = np();
console
.log(myName
,myAge
);
17:rest操作符 …数组名(可不写)(解构完数组的几个元素后,将剩余的元素当成子数组返回回来)
var arr
= [1,2,3,4,5,6];
var [a
,b
, ...rest
] = arr
;
console
.log(a
,b
,rest
);
var [a
, ,b
, ...rest
] = arr
;
console
.log(a
,b
,rest
);
function variousAge(...rest
){
console
.log(rest
);
}
variousAge(1,2,3);
18:多维数组(在数组里嵌套多个数组)
var arr
= []
for(let i
=0;i
<5;i
++){
arr
[i
] = [];
for(let j
=0,j
<4;j
++){
arr
[i
][j
]=i
+ j
;
}
}
console
.log(arr
);
转载请注明原文地址:https://blackberry.8miu.com/read-3043.html