1. 增加
1.1 push( )
从尾部添加,可以添加多个
var arr = [1,2,3,4]
var b = [5,6,7,8]
arr.push(5,6,7,8)
console.log(arr); // [1,2,3,4,5,6,7,8]
arr.push(b) // 不能实现
console.log(arr);
1.2 unshift( )
从头部添加
var arr=[1,2,3,4]
arr.unshift(0)
console.log(arr);
var b=[1,2,3,4]
var a = [5,6,7,8,9]
a.unshift(...b)
console.log(a);
1.3 concat( )
连接两个数组 (不改变原数组的内容)
var arr = [1,2,3];
var c = [4,5,6];
var b = arr.concat(c);
console.log(arr); // [1,2,3]
console.log(b); // [1,2,3,4,5,6]
1.4 splice()
有增加的用法:(startIndex,0,x1,x2…)
var a =[1,2,3,4]
a.splice(1,0,8,9) // [1,8,9,2,3,4]
console.log(a);
1.5 …arr
展开数组
var arr = [1,2,3]
console.log(arr);
console.log(...arr); // 1 2 3
var a=[4,5,6]
var b = [7,8,9]
a.push(...b) // 拼接数组
console.log(a);
2. 删除
2.1 pop( )
从尾部删除
var arr = [1,2,3,4]
arr.pop()
console.log(arr) // [1,2,3]
2.2 shift( )
从头部删除
var arr = [1,2,3,4]
arr.shift()
console.log(arr); // [2,3,4]
2.3 splice()
(startIndex,长度) 定点删除
var arr = [1,2,3,4,5]
arr.splice(3,1)
console.log(arr); // [1,2,3,5]
3. 修改
3.1 splice()
(startIndex,长度,元素…) 修改 先删除后插入
var a =[1,2,3,4,5]
a.splice(2,1,9)
console.log(a); // [1,2,9,4,5]
4. 查询
4.1 arr[index]
查询对应下标的值
//arr[index] 查询对应下标的值
var arr=[1,2,3];
console.log(arr[0]); //1;
4.2 indexOf(value)
查询对应值的下标
var arr=[1,2,3,4];
console.log(arr.indexOf(1)); // 0
//如果没有对应的值,则返回-1
4.3 includes(value)
判断数组是否包含某个值
// 返回boolean
var arr = [1,2,3,4]
console.log(arr.includes(5)); // false
4.4 slice(start,end)
选取 (包含开始,不含结束)
// slice(startIndex,endIndex) 截取 包含开始不含结束
// slice(startIndex) 截取从startIndex开始到后面的所有值
var arr = [1,2,3,4]
console.log(arr.slice(1,3)); // [2,3]
console.log(arr.slice(1)); // [2,3,4]
4.5 join()
将数组拼接为字符串
var arr = ["html","css","javascript"]
console.log(arr.join()); // html,css,javascript
console.log(arr.join("|")); // html|css|javascript
console.log(arr.join("")); // htmlcssjavascript
<p id="p"></p>
var p = document.getElementById("p")
var arr = ["1994","犯罪","剧情"]
var str = arr.join("/")
p.innerHTML = str
console.log(p);