add:数组的其他方法
This commit is contained in:
@@ -85,8 +85,9 @@ ES6中的字符串扩展,用得少,而且逻辑相对简单。如下:
|
||||
|
||||
## 数组的扩展
|
||||
|
||||
### 扩展1
|
||||
> 下面提到的数组的几个方法,更详细的内容,可以看《03-JavaScript基础/17-数组的其他方法.md》。
|
||||
|
||||
### 扩展1:Array.from()
|
||||
|
||||
```javascript
|
||||
Array.from(伪数组/可遍历的对象)
|
||||
@@ -131,8 +132,7 @@ ES6中的字符串扩展,用得少,而且逻辑相对简单。如下:
|
||||
|
||||

|
||||
|
||||
### 扩展2
|
||||
|
||||
### 扩展2:Array.of()
|
||||
|
||||
```javascript
|
||||
Array.of(value1, value2, value3)
|
||||
@@ -149,7 +149,7 @@ ES6中的字符串扩展,用得少,而且逻辑相对简单。如下:
|
||||
console.log(arr);
|
||||
```
|
||||
|
||||
### 扩展3
|
||||
### 扩展3:find() 和 findIndex()
|
||||
|
||||
**方法1**:
|
||||
|
||||
@@ -158,19 +158,7 @@ ES6中的字符串扩展,用得少,而且逻辑相对简单。如下:
|
||||
find(function(item, index, arr){return true})
|
||||
```
|
||||
|
||||
**作用**:找出第一个满足「指定条件返回true」的元素。
|
||||
|
||||
举例:
|
||||
|
||||
```javascript
|
||||
let arr = [2, 3, 2, 5, 7, 6];
|
||||
|
||||
let result = arr.find(function (item, index) {
|
||||
return item > 4; //遍历数组arr,一旦发现有第一个元素大于4,就把这个元素返回
|
||||
});
|
||||
|
||||
console.log(result); //打印结果:5
|
||||
```
|
||||
**作用**:找出**第一个**满足「指定条件返回true」的元素。
|
||||
|
||||
**方法2**:
|
||||
|
||||
@@ -180,19 +168,6 @@ ES6中的字符串扩展,用得少,而且逻辑相对简单。如下:
|
||||
|
||||
**作用**:找出第一个满足「指定条件返回true」的元素的index。
|
||||
|
||||
举例:
|
||||
|
||||
> 我们直接把上面的代码中的find方法改成findIndex即可。
|
||||
|
||||
```javascript
|
||||
let arr = [2, 3, 2, 5, 7, 6];
|
||||
|
||||
let result = arr.findIndex(function (item, index) {
|
||||
return item > 4; //遍历数组arr,一旦发现有第一个元素大于4,就把这个元素的index返回
|
||||
});
|
||||
|
||||
console.log(result); //打印结果:3
|
||||
```
|
||||
|
||||
## 对象的扩展
|
||||
|
||||
|
||||
Reference in New Issue
Block a user