2019-02-03 14:06:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 数组的四个基本方法(数组元素的添加和删除)
|
|
|
|
|
|
|
|
|
|
### push()
|
|
|
|
|
|
|
|
|
|
`push()`:向数组的**最后面**插入一个或多个元素,返回结果为**该数组新的长度**。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
语法:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
数组的新长度 = 数组.push(元素);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
代码举例:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var arr = ["王一", "王二", "王三"];
|
|
|
|
|
|
|
|
|
|
var result1 = arr.push("王四"); // 末尾插入一个元素
|
|
|
|
|
var result2 = arr.push("王五", "王六"); // 末尾插入多个元素
|
|
|
|
|
|
|
|
|
|
console.log(result1); // 打印结果:4
|
|
|
|
|
console.log(result2); // 打印结果:6
|
|
|
|
|
console.log(JSON.stringify(arr)); // 打印结果:["王一","王二","王三","王四","王五","王六"]
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### pop()
|
|
|
|
|
|
|
|
|
|
`pop()`:删除数组中的**最后一个**元素,返回结果为**被删除的元素**。
|
|
|
|
|
|
|
|
|
|
语法:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
被删除的元素 = 数组.pop();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
代码举例:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var arr = ["王一", "王二", "王三"];
|
|
|
|
|
|
|
|
|
|
var result1 = arr.pop();
|
|
|
|
|
|
|
|
|
|
console.log(result1); // 打印结果:王三
|
|
|
|
|
console.log(JSON.stringify(arr)); // 打印结果:["王一","王二"]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### unshift()
|
|
|
|
|
|
|
|
|
|
`unshift()`:在数组**最前面**插入一个或多个元素,返回结果为**该数组新的长度**。插入元素后,其他元素的索引会依次调整。
|
|
|
|
|
|
|
|
|
|
语法:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
数组的新长度 = 数组.unshift(元素);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
代码举例:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var arr = ["王一", "王二", "王三"];
|
|
|
|
|
|
|
|
|
|
var result1 = arr.unshift("王四"); // 最前面插入一个元素
|
|
|
|
|
var result2 = arr.unshift("王五", "王六"); // 最前面插入多个元素
|
|
|
|
|
|
|
|
|
|
console.log(result1); // 打印结果:4
|
|
|
|
|
console.log(result2); // 打印结果:6
|
|
|
|
|
console.log(JSON.stringify(arr)); // 打印结果:["王五","王六","王四","王一","王二","王三"]
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### shift()
|
|
|
|
|
|
|
|
|
|
`shift()`:删除数组中的**第一个**元素,返回结果为**被删除的元素**。
|
|
|
|
|
|
|
|
|
|
语法:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
被删除的元素 = 数组.shift();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
代码举例:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var arr = ["王一", "王二", "王三"];
|
|
|
|
|
|
|
|
|
|
var result1 = arr.shift();
|
|
|
|
|
|
|
|
|
|
console.log(result1); // 打印结果:王一
|
|
|
|
|
console.log(JSON.stringify(arr)); // 打印结果:["王二","王三"]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 数组的遍历
|
|
|
|
|
|
|
|
|
|
遍历数组即:获取并操作数组中的每一个元素。
|
|
|
|
|
|
|
|
|
|
### for循环 遍历
|
|
|
|
|
|
|
|
|
|
举例:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var arr = ["生命壹号","许嵩","永不止步"];
|
|
|
|
|
for(var i = 0;i<arr.length;i++){
|
|
|
|
|
console.log(arr[i]); // arr[i]代表的是数组中的每一个元素i
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log(arr);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
打印结果:
|
|
|
|
|
|
|
|
|
|
![](http://img.smyhvae.com/20180124_2008.png)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### forEach() 遍历
|
|
|
|
|
|
|
|
|
|
> `forEach()` 这种遍历方法只支持IE8以上的浏览器。IE8及以下的浏览器均不支持该方法。所以如果需要兼容IE8,则不要使用forEach,改为使用for循环来遍历即可。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
forEach()方法需要一个函数作为参数。这种函数,是由我们创建但是不由我们调用的,我们称为回调函数。
|
|
|
|
|
|
|
|
|
|
数组中有几个元素,该回调函数就会执行几次。执行完毕后,浏览器会将遍历到的元素。
|
|
|
|
|
|
|
|
|
|
回调函数中传递三个参数:
|
|
|
|
|
|
|
|
|
|
- 第一个参数,就是当前正在遍历的元素
|
|
|
|
|
|
|
|
|
|
- 第二个参数,就是当前正在遍历的元素的索引
|
|
|
|
|
|
|
|
|
|
- 第三个参数,就是正在遍历的数组
|
|
|
|
|
|
|
|
|
|
代码举例:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var arr = ["王一", "王二", "王三"];
|
|
|
|
|
|
|
|
|
|
arr.forEach(function(item, index, obj) {
|
|
|
|
|
console.log("item:" + item);
|
|
|
|
|
console.log("index:" + index);
|
|
|
|
|
console.log("obj:" + obj);
|
|
|
|
|
console.log("----------");
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
打印结果:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
item:王一
|
|
|
|
|
index:0
|
|
|
|
|
obj:王一,王二,王三
|
|
|
|
|
----------
|
|
|
|
|
|
|
|
|
|
item:王二
|
|
|
|
|
index:1
|
|
|
|
|
obj:王一,王二,王三
|
|
|
|
|
----------
|
|
|
|
|
|
|
|
|
|
item:王三
|
|
|
|
|
index:2
|
|
|
|
|
obj:王一,王二,王三
|
|
|
|
|
----------
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
2019-02-03 19:50:42 +08:00
|
|
|
|
### map()方法
|
|
|
|
|
|
|
|
|
|
解释:对数组中每一项运行回调函数,返回该函数的结果,组成的新数组(返回的是**加工之后**的新数组)。
|
|
|
|
|
|
|
|
|
|
比如说,有一个已知的数组arr1,我要求让arr1中的每个素加10,这里就可以用到map方法。举例:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var arr1 = [1, 3, 6, 2, 5, 6];
|
|
|
|
|
|
|
|
|
|
var arr2 = arr1.map(function (item, index) {
|
|
|
|
|
return item + 10; //让arr1中的每个元素加10
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
console.log(arr2);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
打印结果:
|
|
|
|
|
|
|
|
|
|
![](http://img.smyhvae.com/20180402_0938.png)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
举例2:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var arr1 = ["千古", "宿敌", "南山忆", "素颜"];
|
|
|
|
|
|
|
|
|
|
var arr2 = arr1.map(function (element, index, array) {
|
|
|
|
|
return element + "vae"; //给arr1中所有的元素增加字符串"vae",放到arr2中。
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(arr1);
|
|
|
|
|
console.log(arr2);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
结果:
|
|
|
|
|
|
|
|
|
|
![](http://img.smyhvae.com/20180126_1425.png)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### filter
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
举例:让找出数组arr1中大于4的元素,返回一个新的数组。代码如下:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var arr1 = [1, 3, 6, 2, 5, 6];
|
|
|
|
|
|
|
|
|
|
var arr2 = arr1.filter(function (item, index) {
|
|
|
|
|
return item > 4; //将arr1中大于4的元素返回
|
|
|
|
|
})
|
|
|
|
|
console.log(arr2);
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
打印结果:
|
|
|
|
|
|
|
|
|
|
![](http://img.smyhvae.com/20180402_0951.png)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
Array.prototype.filter(function(item, index){})
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
作用:遍历过滤出一个新的子数组,返回条件为true的值。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 数组迭代方法
|
|
|
|
|
|
|
|
|
|
数组迭代方法包括:every()、filter()、forEach()、map()、some()
|
|
|
|
|
|
|
|
|
|
PS:这几个方法**不会修改原数组**。
|
|
|
|
|
|
|
|
|
|
语法格式:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
数组/boolean/无 = 数组.every/filter/forEach/map/some(
|
|
|
|
|
function(element,index,arr){
|
|
|
|
|
程序和返回值;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
有了这几种方法,就可以替代一些for循环了。下面依次来介绍。
|
|
|
|
|
|
|
|
|
|
### every()方法
|
|
|
|
|
|
|
|
|
|
解释:对数组中每一项运行回调函数,如果都返回true,every就返回true;如果有一项返回false,则停止遍历,此方法返回false。
|
|
|
|
|
|
|
|
|
|
注意:every()方法的返回值是boolean值,参数是回调函数。
|
|
|
|
|
|
|
|
|
|
举例:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var arr1 = ["千古", "宿敌", "南山忆", "素颜"];
|
|
|
|
|
var bool1 = arr1.every(function (element, index, array) {
|
|
|
|
|
if (element.length > 2) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
console.log(bool1); //输出结果:false。只要有一个元素的长度是超过两个字符的,就返回false
|
|
|
|
|
|
|
|
|
|
var arr2 = ["千古", "宿敌", "南山", "素颜"];
|
|
|
|
|
var bool2 = arr2.every(function (element, index, array) {
|
|
|
|
|
if (element.length > 2) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
console.log(bool2); //输出结果:true。因为每个元素的长度都是两个字符。
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### some()方法
|
|
|
|
|
|
|
|
|
|
解释:对数组中每一项运行回调函数,只要有一项返回true,则停止遍历,此方法返回true。
|
|
|
|
|
|
|
|
|
|
### filter()方法
|
|
|
|
|
|
|
|
|
|
解释:对数组中每一项运行回调函数,该函数返回结果是true的项,将组成新的数组(返回值就是这个新的数组)。
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
var arr1 = ["千古", "宿敌", "南山忆", "素颜"];
|
|
|
|
|
|
|
|
|
|
var arr2 = arr1.filter(function (element, index, array) {
|
|
|
|
|
if (element.length > 2) { //arr1中的元素,如果是长度超过2个字符的,我就把它放到arr2中去
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
console.log(arr1);
|
|
|
|
|
console.log(arr2);
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
结果:
|
|
|
|
|
|
|
|
|
|
![](http://img.smyhvae.com/20180126_1410.png)
|
|
|
|
|
|
2019-02-03 14:06:03 +08:00
|
|
|
|
|
|
|
|
|
|
2019-02-03 18:06:24 +08:00
|
|
|
|
## 我的公众号
|
|
|
|
|
|
|
|
|
|
想学习<font color=#0000ff>**代码之外的技能**</font>?不妨关注我的微信公众号:**千古壹号**(id:`qianguyihao`)。
|
|
|
|
|
|
|
|
|
|
扫一扫,你将发现另一个全新的世界,而这将是一场美丽的意外:
|
|
|
|
|
|
|
|
|
|
![](http://img.smyhvae.com/2016040102.jpg)
|
|
|
|
|
|
|
|
|
|
|
2019-02-03 14:06:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|