fix(guide): simplify directory structure

This commit is contained in:
Mrugesh Mohapatra
2018-10-16 21:26:13 +05:30
parent f989c28c52
commit da0df12ab7
35752 changed files with 0 additions and 317652 deletions

View File

@@ -0,0 +1,91 @@
---
title: Array Destructuring
localeTitle: 数组解构
---
# 数组解构
解构是从存储在数组中的数据中提取多个值的便捷方式。它可以在接收数据的位置使用(例如,分配的左侧)。 `ECMAScript 6`引入了此功能。
如何提取值通过模式指定(请参阅示例)。
### 基本变量赋值
```
var names = ['neel', 'meet', 'darshan'];
var [nameOne, nameTwo, nameThree] = names;
console.log(nameOne); // "neel"
console.log(nameTwo); // "meet"
console.log(nameThree); // "darshan"
```
### 与声明分开的作业
变量可以通过与变量声明分开的解构来赋值。
```
var a, b;
[a, b] = [1, 2];
console.log(a); // 1
console.log(b); // 2
```
### 默认值
如果`undefined`从数组中解压缩的值,则可以为变量分配默认值。
```
var a, b;
[a=5, b=7] = [1];
console.log(a); // 1
console.log(b); // 7
```
### 解析从函数返回的数组
始终可以从函数返回数组。解构可以使数组返回值更简洁。
在此示例中, `getNames()`返回值`['neel', 'meet']`作为其输出,可以使用解构在单行中进行解析。
```
function getNames() {
return ['neel', 'meet'];
}
var neel, meet;
[nameOne, nameTwo] = getNames();
console.log(nameOne); // neel
console.log(nameTwo); // meet
```
### 忽略一些返回的值
您可以忽略您不感兴趣的返回值:
```
function getNames() {
return ['neel', 'meet', 'darshan'];
}
var [nameOne, , nameThree] = getNames();
console.log(nameOne); // neel
console.log(nameThree); // darshan
```
您还可以忽略所有返回的值:
```
[,,] = getNames();
```
### 将数组的其余部分分配给变量
解构数组时可以使用rest模式解压缩并将其余部分分配给变量
```
var [a, ...b] = [1, 2, 3];
console.log(a); // 1
console.log(b); // [2, 3]
```
请注意如果在左侧使用带有rest元素的尾随逗号则将抛出`SyntaxError`
```
var [a, ...b,] = [1, 2, 3];
// SyntaxError: rest element may not have a trailing comma
```
另请参见: **数组解构** | [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Array_destructuring)

View File

@@ -0,0 +1,19 @@
---
title: Array from
localeTitle: 数组来自
---
## 数组来自
'Array.from'方法从类似数组或可迭代的对象创建一个新的Array实例。
### 句法:
“”” Array.fromarrayLike \[mapFn \[thisArg\]\] “””
### 例:
“”” Array.from '富'; // \[“f”“o”“o”\] “””
#### 更多信息:
[MDN文档](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from)

View File

@@ -0,0 +1,38 @@
---
title: Array isArray
localeTitle: Array isArray
---
如果对象是数组,则`Array.isArray()`方法返回`true`否则返回`false`
## 句法
```
Array.isArray(obj)
```
### 参数
**obj**要检查的对象。
[MDN链接](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray) | [MSDN链接](https://msdn.microsoft.com/en-us/LIBRary/ff848265%28v=vs.94%29.aspx)
## 例子
```
// all following calls return true
Array.isArray([]);
Array.isArray([1]);
Array.isArray(new Array());
// Little known fact: Array.prototype itself is an array:
Array.isArray(Array.prototype);
// all following calls return false
Array.isArray();
Array.isArray({});
Array.isArray(null);
Array.isArray(undefined);
Array.isArray(17);
Array.isArray('Array');
Array.isArray(true);
Array.isArray(false);
Array.isArray({ __proto__: Array.prototype });
```

View File

@@ -0,0 +1,26 @@
---
title: Array Length
localeTitle: 数组长度
---
## 数组长度
`length`是JavaScript中数组的一个属性它返回或设置给定数组中的元素数。
可以像这样返回数组的`length`属性。
```js
let desserts = ["Cake", "Pie", "Brownies"];
console.log(desserts.length); // 3
```
赋值运算符与`length`属性一起可用于设置数组中的元素数量,如此。
```js
let cars = ["Saab", "BMW", "Volvo"];
cars.length = 2;
console.log(cars.length); // 2
```
#### 更多信息:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/Array/length

View File

@@ -0,0 +1,27 @@
---
title: Array of
localeTitle: 数组
---
## 数组
无论参数的数量或类型如何Array.of方法都会创建一个具有可变数量参数的新Array实例。
句法:
```javascript
Array.of(element0[, element1[, ...[, elementN]]])
```
## 例
```javascript
Array.of(7); // [7] - creates an array with a single element
Array.of(1, 2, 3); // [1, 2, 3]
Array(7); // [ , , , , , , ] - creates an empty array with a length property of 7
Array(1, 2, 3); // [1, 2, 3]
```
#### 更多信息:
有关更多信息,请访问[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/of)

View File

@@ -0,0 +1,45 @@
---
title: Array.prototype.concat
localeTitle: Array.prototype.concat
---
## Array.prototype.concat
'concat'方法返回一个新数组,该数组由您调用它的数组元素组成,后跟参数传递顺序的元素。
您可以将多个参数传递给'concat'方法。参数可以是数组,也可以是布尔值,字符串和数字等数据类型。
### 句法
```javascript
const newArray = array.concat(value1, value2, value3...);
```
### 例子
#### 连接两个数组
```javascript
var cold = ['Blue', 'Green', 'Purple'];
var warm = ['Red', 'Orange', 'Yellow'];
var result = cold.concat(warm);
console.log(result);
// results in ['Blue', 'Green', 'Purple', 'Red', 'Orange', 'Yellow'];
```
#### 将值连接到数组
```javascript
const odd = [1, 3, 5, 7, 9];
const even = [0, 2, 4, 6, 8];
const oddAndEvenAndTen = odd.concat(even, 10);
console.log(oddAndEvenAndTen);
// results in [1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 10];
```
#### 更多信息:
[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat)

View File

@@ -0,0 +1,11 @@
---
title: Array.prototype.copyWithin
localeTitle: Array.prototype.copyWithin
---
## Array.prototype.copyWithin
这是一个存根。 [帮助我们的社区扩展它](https://github.com/freecodecamp/guides/tree/master/src/pages/javascript/standard-objects/array/array-prototype-copywithin/index.md) 。
[这种快速风格指南有助于确保您的拉取请求被接受](https://github.com/freecodecamp/guides/blob/master/README.md) 。
#### 更多信息:

View File

@@ -0,0 +1,11 @@
---
title: Array.prototype.entries
localeTitle: Array.prototype.entries
---
## Array.prototype.entries
这是一个存根。 [帮助我们的社区扩展它](https://github.com/freecodecamp/guides/tree/master/src/pages/javascript/standard-objects/array/array-prototype-entries/index.md) 。
[这种快速风格指南有助于确保您的拉取请求被接受](https://github.com/freecodecamp/guides/blob/master/README.md) 。
#### 更多信息:

View File

@@ -0,0 +1,74 @@
---
title: Array.prototype.every
localeTitle: Array.prototype.every
---
`every()`方法测试数组中的所有元素是否都通过了由提供的函数实现的测试。
**句法**
```javascript
arr.every(callback[, thisArg])
```
## 参数
* **callback**函数测试每个元素,取三个参数:
* **currentValue** (必填)
当前元素在数组中处理。
* **索引** (可选)
数组中正在处理的当前元素的索引。
* **数组** (可选)
每个阵列都被调用了。
* **thisArg**可选。执行回调时要使用的值。
[MDN链接](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every) | [MSDN链接](https://msdn.microsoft.com/en-us/LIBRary/ff679981%28v=vs.94%29.aspx)
## 描述
`every`方法按升序索引顺序为每个数组元素调用`every` `callback`函数,直到`callback`函数返回false。如果找到导致`callback`返回false的元素则every方法立即返回`false` 。否则every方法返回`true`
不会为缺少的数组元素调用回调函数。
除了数组对象之外每个方法都可以被具有length属性且具有数字索引属性名称的任何对象使用。 `every`都不会改变调用它的数组。
## 例子
```javascript
function isBigEnough(element, index, array) {
return element >= 10;
}
[12, 5, 8, 130, 44].every(isBigEnough); // false
[12, 54, 18, 130, 44].every(isBigEnough); // true
// Define the callback function.
function CheckIfEven(value, index, ar) {
document.write(value + " ");
if (value % 2 == 0)
return true;
else
return false;
}
// Create an array.
var numbers = [2, 4, 5, 6, 8];
// Check whether the callback function returns true for all of the
// array values.
if (numbers.every(CheckIfEven))
document.write("All are even.");
else
document.write("Some are not even.");
// Output:
// 2 4 5 Some are not even.
```

View File

@@ -0,0 +1,26 @@
---
title: Array.prototype.fill
localeTitle: Array.prototype.fill
---
## Array.prototype.fill
fill方法使用静态值填充数组中的所有元素。
句法:
\`\`\`\`javascript arr.fill arr.fill开始 arr.fill开始结束
```
The fill method takes up to three arguments value, start and end. The start and end arguments are optional with default values of 0 and the length of the this object.
The fill method is a mutable method, it will change this object itself, and return it, not just return a copy of it.
## Examples
```
JavaScript的 \[1,2,3\] .fill4; // \[4,4,4\] \[1,2,3\] .fill4,1; // \[1,4,4\]
var fruits = \[“Grape”“Pear”“Apple”“Strawberry”\]; fruits.fill“西瓜”2,4; //香蕉,梨,西瓜,西瓜 \`\`\`
#### 更多信息:
有关更多信息,请访问[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill)

View File

@@ -0,0 +1,78 @@
---
title: Array.prototype.filter
localeTitle: Array.prototype.filter
---
## Array.prototype.filter
filter方法将数组作为输入。它接受数组中的每个元素并对它应用条件语句。如果此条件返回true则元素被“推送”到输出数组。
一旦输入数组中的每个元素都被“过滤”它就会输出一个包含返回true的每个元素的新数组。
在下面的示例中有一个数组其中包含多个对象。通常要遍历此数组您可以使用for循环。
在这种情况下我们希望得到所有成绩大于或等于90的学生。
```javascript
var students = [
{ name: 'Quincy', grade: 96 },
{ name: 'Jason', grade: 84 },
{ name: 'Alexis', grade: 100 },
{ name: 'Sam', grade: 65 },
{ name: 'Katie', grade: 90 }
];
//Define an array to push student objects to.
var studentsGrades = []
for (var i = 0; i < students.length; i++) {
//Check if grade is greater than 90
if (students[i].grade >= 90) {
//Add a student to the studentsGrades array.
studentsGrades.push(students[i])
}
}
return studentsGrades; // [ { name: 'Quincy', grade: 96 }, { name: 'Alexis', grade: 100 }, { name: 'Katie', grade: 90 } ]
```
这个for循环有效但它非常冗长。对于需要迭代的许多数组一遍又一遍地编写循环也会变得很繁琐。
这是过滤器的一个很好的用例!
以下是使用过滤器的相同示例:
```javascript
var students = [
{ name: 'Quincy', grade: 96 },
{ name: 'Jason', grade: 84 },
{ name: 'Alexis', grade: 100 },
{ name: 'Sam', grade: 65 },
{ name: 'Katie', grade: 90 }
];
var studentGrades = students.filter(function (student) {
//This tests if student.grade is greater than or equal to 90. It returns the "student" object if this conditional is met.
return student.grade >= 90;
});
return studentGrades; // [ { name: 'Quincy', grade: 96 }, { name: 'Alexis', grade: 100 }, { name: 'Katie', grade: 90 } ]
```
编写过滤器的方法要快得多读取时更清晰同时仍然可以完成同样的事情。使用ES6语法我们甚至可以使用过滤器复制6行for循环
```javascript
var students = [
{ name: 'Quincy', grade: 96 },
{ name: 'Jason', grade: 84 },
{ name: 'Alexis', grade: 100 },
{ name: 'Sam', grade: 65 },
{ name: 'Katie', grade: 90 }
];
var studentGrades = students.filter(student => student.grade >= 90);
return studentGrades; // [ { name: 'Quincy', grade: 96 }, { name: 'Alexis', grade: 100 }, { name: 'Katie', grade: 90 } ]
```
过滤器非常有用是一个很好的选择for循环来过滤数组对条件语句。
#### 更多信息:
[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)

View File

@@ -0,0 +1,71 @@
---
title: Array.prototype.find
localeTitle: Array.prototype.find
---
## 信息
`find()`方法返回数组中第一个满足提供的测试函数的元素的值。否则返回undefined。 `find()`方法不会改变调用它的数组。
句法:
```
arr.find(callback[, thisArg])
```
##### 参数
* `callback`
* 函数对数组中的每个值执行,取三个参数:
* `element`
* 当前元素在数组中处理。
* `index`
* 数组中正在处理的当前元素的索引。
* `array`
* 数组查找被调用。
* `thisArg` (可选)
* 执行回调时要用作此对象的对象。
##### 返回值
元素通过测试时数组中的值;否则,未定义。
[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)
## 例子
此示例将在数组中找到相应的项并从中返回对象。
```javascript
let items = [
{name: 'books', quantity: 2},
{name: 'movies', quantity: 1},
{name: 'games', quantity: 5}
];
function findMovies(item) {
return item.name === 'movies';
}
console.log(items.find(findMovies));
// Output
// { name: 'movies', quantity: 1 }
```
以下示例显示了回调函数的每个可选参数的输出。这将返回`undefined`因为没有任何项将从回调函数返回true。
```javascript
function showInfo(element, index, array) {
console.log('element = ' + element + ', index = ' + index + ', array = ' + array);
return false;
}
console.log('return = ' + [4, 6, 8, 12].find(showInfo));
// Output
// element = 4, index = 0, array = 4,6,8,12
// element = 6, index = 1, array = 4,6,8,12
// element = 8, index = 2, array = 4,6,8,12
// element = 12, index = 3, array = 4,6,8,12
// return = undefined
```

View File

@@ -0,0 +1,73 @@
---
title: Array.prototype.findIndex
localeTitle: Array.prototype.findIndex
---
## 信息
`findIndex()`方法返回数组中第一个满足提供的测试函数的元素的索引。否则返回-1。
`findIndex()`方法不会改变调用它的数组。
句法:
```
arr.findIndex(callback[, thisArg])
```
##### 参数
* `callback`
* 函数对数组中的每个值执行,取三个参数:
* `element`
* 当前元素在数组中处理。
* `index`
* 数组中正在处理的当前元素的索引。
* `array`
* 调用了数组findIndex
* `thisArg` (可选)
* 执行回调时要用作此对象的对象。
##### 返回值
如果元素通过测试,则为数组中的索引;否则,-1。
[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex)
## 例子
此示例将在数组中找到相应的项并从中返回索引。
```javascript
let items = [
{name: 'books', quantity: 2},
{name: 'movies', quantity: 1},
{name: 'games', quantity: 5}
];
function findMovies(item) {
return item.name === 'movies';
}
console.log(items.findIndex(findMovies));
// Index of 2nd element in the Array is returned,
// so this will result in '1'
```
以下示例显示了回调函数的每个可选参数的输出。这将返回`-1`因为没有任何项将从回调函数返回true。
```javascript
function showInfo(element, index, array) {
console.log('element = ' + element + ', index = ' + index + ', array = ' + array);
return false;
}
console.log('return = ' + [4, 6, 8, 12].findIndex(showInfo));
// Output
// element = 4, index = 0, array = 4,6,8,12
// element = 6, index = 1, array = 4,6,8,12
// element = 8, index = 2, array = 4,6,8,12
// element = 12, index = 3, array = 4,6,8,12
// return = -1
```

View File

@@ -0,0 +1,37 @@
---
title: Array.prototype.forEach
localeTitle: Array.prototype.forEach
---
## Array.prototype.forEach
'forEach'数组方法用于迭代数组中的每个项。该方法在数组Object上调用并传递一个函数该函数在数组中的每个项目上调用。
```javascript
var arr = [1, 2, 3, 4, 5];
arr.forEach(number => console.log(number * 2));
// 2
// 4
// 6
// 8
// 10
```
如果需要引用数组中当前项的索引,则回调函数还可以获取索引的第二个参数。
```javascript
var arr = [1, 2, 3, 4, 5];
arr.forEach((number, i) => console.log(`${number} is at index ${i}`));
// '1 is at index 0'
// '2 is at index 1'
// '3 is at index 2'
// '4 is at index 3'
// '5 is at index 4'
```
#### 更多信息:
[关于Array.prototype.forEach的MDN文章](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)

View File

@@ -0,0 +1,24 @@
---
title: Array.prototype.includes
localeTitle: Array.prototype.includes
---
## Array.prototype.includes
`includes()`方法确定数组是否包含值。它返回true或false。
它需要两个参数:
1. `searchValue` - 要在数组中搜索的元素。
2. `fromIndex` - 数组中开始搜索proivded `searchValue` 。如果提供负值,则从数组的长度减去负值开始。
### 例
```js
const a = [1, 2, 3];
a.includes(2); // true
a.includes(4); // false
```
#### 更多信息:
[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes)

View File

@@ -0,0 +1,44 @@
---
title: Array.prototype.indexOf
localeTitle: Array.prototype.indexOf
---
## Array.prototype.indexOf
`indexOf()`方法返回可在数组中找到给定元素的第一个索引。如果该元素不存在,则返回-1。
**句法**
```javascript
arr.indexOf(searchElement[, fromIndex])
```
## 参数
* **searchElement**您正在查找的元素
* **fromIndex**可选。要在其中开始搜索的索引。如果fromIndex大于或等于数组的长度则不搜索该数组并且该方法返回-1。如果fromIndex是负数则它会考虑从数组末尾开始的偏移量数组仍然从那里向前搜索。默认值为0表示搜索整个数组。
## 描述
`indexOf`方法以递增索引顺序获取每个数组元素,并使用严格相等( `===` )对`searchElement`进行检查。一旦找到返回`true`的元素,它就会返回其索引。
## 例子
```javascript
var array = [1, 2, 4, 1, 7]
array.indexOf(1); // 0
array.indexOf(7); // 4
array.indexOf(6); // -1
array.indexOf('1'); // -1
array.indexOf('hello'); // -1
array.indexOf(1, 2); // 3
array.indexOf(1, -3); // 3
```
### 更多信息:
[MDN链接](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)
[MSDN链接](https://docs.microsoft.com/en-us/scripting/javascript/reference/indexof-method-array-javascript)

View File

@@ -0,0 +1,39 @@
---
title: Array.prototype.join
localeTitle: Array.prototype.join
---
JavaScript数组方法`.join()`将数组的所有元素组合成一个字符串。
**句法**
```javascript
var array = ["Lorem", "Ipsum", "Dolor", "Sit"];
var str = array.join([separator]);
```
## 参数
**分隔器**
可选的。指定用于分隔原始数组的每个元素的字符串。如果分隔符不是字符串则将其转换为字符串。如果未提供separator参数则默认情况下使用逗号分隔数组元素。如果separator是一个空字符串`""` ,则所有数组元素之间的连接都没有分隔符。
## 描述
`.join()`将数组的所有元素连接成一个字符串。如果任何数组元素`undefined`或为`null` ,则该元素将转换为空字符串`""`
## 例子
**使用`.join()`四种不同的方式**
```javascript
var array = ["Lorem", "Ipsum", "Dolor" ,"Sit"];
var join1 = array.join(); /* assigns "Lorem,Ipsum,Dolor,Sit" to join1 variable
(because no separator was provided .join()
defaulted to using a comma) */
var join2 = array.join(", "); // assigns "Lorem, Ipsum, Dolor, Sit" to join2 variable
var join3 = array.join(" + "); // assigns "Lorem + Ipsum + Dolor + Sit" to join3 variable
var join4 = array.join(""); // assigns "LoremIpsumDolorSit" to join4 variable
```
资料来源: [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join)

View File

@@ -0,0 +1,75 @@
---
title: Array.prototype.lastIndexOf
localeTitle: Array.prototype.lastIndexOf
---
## Array.prototype.lastIndexof
`lastIndexOf()`方法返回在数组中可以找到给定元素的最后一个索引,如果不存在则返回-1。从`fromIndex`开始向后搜索数组。
**句法**
```javascript
arr.lastIndexOf(searchElement, fromIndex = arr.length - 1])
```
## 参数
* **searchElement**
* 要在数组中定位的元素。
* **的fromIndex**
* _可选_ 。要开始向后搜索的索引。默认为数组的长度减去1即将搜索整个数组。如果索引大于或等于数组的长度则将搜索整个数组。如果是负数则将其作为距离数组末尾的偏移量。请注意即使索引为负数仍会从后向前搜索数组。如果计算的索引小于0则返回-1即不搜索数组。
[MDN链接](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf) | [MSDN链接](https://msdn.microsoft.com/en-us/LIBRary/ff679972%28v=vs.94%29.aspx)
## 返回
数组中最后一次出现`searchElement`的索引,如果未找到`searchElement` ,则`searchElement` -1。
## 描述
`lastIndexOf`使用严格相等(与===或`searchElement`运算符使用的方法相同)将`searchElement`与Array的元素进行比较。
## 备注
搜索以降序索引顺序(最后一个成员优先)发生。要按升序搜索,请使用`indexOf`方法。
可选的`fromIndex`参数指定开始搜索的数组索引。如果`fromIndex`大于或等于数组长度,则搜索整个数组。如果`fromIndex`为负数,则搜索从数组长度加上`fromIndex` 。如果计算的索引小于0则返回-1。
## 例子
```javascript
var array = [2, 5, 9, 2];
array.lastIndexOf(2); // 3
array.lastIndexOf(7); // -1
array.lastIndexOf(2, 3); // 3
array.lastIndexOf(2, 2); // 0
array.lastIndexOf(2, -2); // 0
array.lastIndexOf(2, -1); // 3
// Create an array.
var ar = ["ab", "cd", "ef", "ab", "cd"];
// Determine the first location, in descending order, of "cd".
document.write(ar.lastIndexOf("cd") + "<br/>");
// Output: 4
// Find "cd" in descending order, starting at index 2.
document.write(ar.lastIndexOf("cd", 2) + "<br/>");
// Output: 1
// Search for "gh" (which is not found).
document.write(ar.lastIndexOf("gh")+ "<br/>");
// Output: -1
// Find "ab" with a fromIndex argument of -3.
// The search in descending order starts at index 3,
// which is the array length minus 2.
document.write(ar.lastIndexOf("ab", -3) + "<br/>");
// Output: 0
```

View File

@@ -0,0 +1,32 @@
---
title: Array.prototype.map
localeTitle: Array.prototype.map
---
## Array.prototype.map
`.map()`方法遍历给定数组并在每个元素上执行提供的函数。它返回一个新数组,其中包含每个元素上函数调用的结果。
### 例子
**ES5**
```js
var arr = [1, 2, 3, 4];
var newArray = arr.map(function(element) { return element * 2});
console.log(newArray); // [2, 4, 6, 8]
```
**ES6**
```js
const arr = [1, 2, 3, 4];
const newArray = arr.map(element => element * 2);
console.log(newArray);
//[2, 4, 6, 8]
```
**更多信息**
这是一个交互式的Scrimba截屏视频它解释了`Array.prototype.map()`
[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)

View File

@@ -0,0 +1,37 @@
---
title: Array.prototype.pop
localeTitle: Array.prototype.pop
---
# Array.prototype.pop
`pop()`方法从中删除最后一个元素并更改数组的长度。
**句法**
```js
arr.pop()
```
**返回值**
* 从数组中删除的元素;如果数组为空则为undefined。
## 描述
`pop()`方法从数组中删除最后一个元素,并将该值返回给调用者。
如果在空数组上调用`pop()` 则返回undefined。
## 例子
```js
let array = [1, 2, 3, 4];
array.pop(); // removes 4
console.log(array); // [1, 2, 3]
[].pop() // undefined
```
#### 更多信息:
[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop)

View File

@@ -0,0 +1,52 @@
---
title: Array.prototype.push
localeTitle: Array.prototype.push
---
`push()`方法用于将一个或多个新元素添加到数组的末尾。 它还返回数组的新长度。
### 句法
```javascript
arr.push([element1[, ...[, elementN]]])
```
### 参数
* **elementN**要添加到数组末尾的元素。
### 返回值
调用方法的数组的新长度。
## 描述
`push()`方法将元素_推_送到数组的末尾。它可以采用零个或多个参数。如果没有提供参数 它只会返回数组的当前长度。如果提供了一个或多个参数,它会将这些参数添加到数组中 按照它们的写入顺序。
在将元素推送到数组后,此方法还返回数组的新长度。
## 例:
```javascript
var myStarkFamily = ['John', 'Robb', 'Sansa', 'Bran'];
```
假设你有一系列来自权力的游戏中的House Stark的孩子们。然而其中一名成员**Arya**失踪了。 知道上面的代码,你可以通过在最后一个索引之后的索引处将`'Arya'`分配给数组来添加她,如下所示:
```javascript
myStarkFamily[4] = 'Arya';
```
该解决方案的问题在于它无法处理一般情况。如果您事先不知道阵列的长度是多少, 你不能用这种方式添加新元素。这就是`push()`的用途。我们不需要知道数组有多长。我们只是添加 我们的元素到数组的末尾。
```javascript
myStarkFamily.push('Arya');
console.log(myStarkFamily); // ['John', 'Robb', 'Sansa', 'Bran', 'Arya']
var newLength = myStarkFamily.push('Rickon'); // oops! forgot Rickon
console.log(newLength); // 6
console.log(myStarkFamily); // ['John', 'Robb', 'Sansa', 'Bran', 'Arya', 'Rickon']
```
#### 更多信息:
[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push)

View File

@@ -0,0 +1,67 @@
---
title: Array.prototype.reduce
localeTitle: Array.prototype.reduce
---
## Array.prototype.reduce
`reduce()`方法将值数组减少到只有一个值。
返回的单个值可以是任何类型。
### 例1
将整数数组转换为数组中所有整数的总和。
```js
var numbers = [1,2,3];
var sum = numbers.reduce(function(total, current){
return total + current;
});
console.log(sum);
```
这将输出`6`到控制台。
### 描述
`reduce()`方法被称为阵列转换方法的瑞士军刀或多工具。其他的,如`map()``filter()` ,提供更具体的转换,而`reduce()`可用于将数组转换为您想要的任何输出。
### 句法
```js
arr.reduce(callback[, initialValue])
```
* `callback`参数是一个函数,将为数组中的每个项调用一次。此函数有四个参数,但通常只使用前两个参数。
* _accumulator_ - 上一次迭代的返回值
* _currentValue_ - 数组中的当前项
* _index_ - 当前项的索引
* _array_ - 调用reduce的原始数组
* `initialValue`参数是可选的。如果提供它将在第一次调用回调函数时用作初始累加器值参见下面的示例2
### 例2
将字符串数组转换为单个对象该对象显示每个字符串在数组中出现的次数。注意这个reduce调用将一个空对象`{}`作为`initialValue`参数传递。这将用作传递给回调函数的累加器(第一个参数)的初始值。
```js
var pets = ['dog', 'chicken', 'cat', 'dog', 'chicken', 'chicken', 'rabbit'];
var petCounts = pets.reduce(function(obj, pet){
if (!obj[pet]) {
obj[pet] = 1;
} else {
obj[pet]++;
}
return obj;
}, {});
console.log(petCounts);
```
输出: `js { dog: 2, chicken: 3, cat: 1, rabbit: 1 }`
## 更多信息:
* [JavaScript的Reduce方法如何工作何时使用它以及它可以做的一些很酷的事情](https://medium.freecodecamp.org/reduce-f47a7da511a9)
* [高级减少](https://www.youtube.com/watch?v=1DMolJ2FrNY)
* [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce)

View File

@@ -0,0 +1,11 @@
---
title: Array.prototype.reduceRight
localeTitle: Array.prototype.reduceRight
---
## Array.prototype.reduceRight
这是一个存根。 [帮助我们的社区扩展它](https://github.com/freecodecamp/guides/tree/master/src/pages/javascript/standard-objects/array/array-prototype-reduceright/index.md) 。
[这种快速风格指南有助于确保您的拉取请求被接受](https://github.com/freecodecamp/guides/blob/master/README.md) 。
#### 更多信息:

View File

@@ -0,0 +1,34 @@
---
title: Array.prototype.reverse
localeTitle: Array.prototype.reverse
---
JavaScript数组方法`.reverse()`将颠倒数组中元素的顺序。
**句法**
```javascript
var array = [1, 2, 3, 4, 5];
array.reverse();
```
## 描述
`.reverse()`反转数组元素的索引。
## 例子
**使用`.reverse()`来反转数组的元素**
```javascript
var array = [1, 2, 3, 4, 5];
console.log(array);
// Console will output 1, 2, 3, 4, 5
array.reverse();
console.log(array);
/* Console will output 5, 4, 3, 2, 1 and
the variable array now contains the set [5, 4, 3, 2, 1] */
```
资料来源: [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse)

View File

@@ -0,0 +1,38 @@
---
title: Array.prototype.shift
localeTitle: Array.prototype.shift
---
JavaScript数组方法`.shift()`将从数组中删除第一个元素并返回该值。这也将改变数组的长度
**句法**
```javascript
var array = [1, 2, 3, 4];
array.shift();
```
## 描述
`.shift()`将删除调用它的数组的索引0处的元素。然后它返回删除的值并将所有剩余元素向下移动1个索引值。
如果调用它的数组不包含任何元素,则`.shift()`将返回`undefined`
## 例子
**从数组中移出第一个值**
```javascript
var array = [1, 2, 3, 4, 5];
console.log(array);
// Console will output 1, 2, 3, 4, 5
array.shift();
// If we console.log(array.shift()); the console would output 1.
console.log(array);
/* Console will output 2, 3, 4, 5 and
the variable array now contains the set [2, 3, 4, 5] where
each element has been moved down 1 index value. */
```
资料来源: [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift)

View File

@@ -0,0 +1,44 @@
---
title: Array.prototype.slice
localeTitle: Array.prototype.slice
---
JavaScript数组方法`.slice()`将返回一个新的数组对象,该对象将是原始数组的一个段(一个切片)。原始数组未被修改。
**句法**
```javascript
array.slice()
arr.slice(startIndex)
arr.slice(startIndex, endIndex)
```
## 参数
* **startIndex**切片应开始的从零开始的索引。如果省略该值则它将从0开始。
* **endIndex**切片将**在**此基于零的索引**之前**结束。负索引用于从数组的末尾偏移。如果省略该值,则段将切片到数组的末尾。
## 例子
```javascript
var array = ['books', 'games', 'cup', 'sandwich', 'bag', 'phone', 'cactus']
var everything = array.slice()
// everything = ['books', 'games', 'cup', 'sandwich', 'bag', 'phone', 'cactus']
var kitchen = array.slice(2, 4)
// kitchen = ['cup', 'sandwich']
var random = array.slice(4)
// random = ['bag', 'phone', 'cactus']
var noPlants = array.slice(0, -1)
// noPlats = ['books', 'games', 'cup', 'sandwich', 'bag', 'phone']
// array will still equal ['books', 'games', 'cup', 'sandwich', 'bag', 'phone', 'cactus']
```
#### 更多信息:
资料来源: [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice)

View File

@@ -0,0 +1,92 @@
---
title: Array.prototype.some
localeTitle: Array.prototype.some
---
JavaScript数组方法`.some()`将使用回调函数来测试数组中的每个元素;一旦回调返回`true``.some()`将立即返回true。
**句法**
```javascript
var arr = [1, 2, 3, 4];
arr.some(callback[, thisArg]);
```
## 回调函数
**句法**
```javascript
var isEven = function isEven(currentElement, index, array) {
if(currentElement % 2 === 0) {
return true;
} else {
return false;
}
}
```
请参阅[算术运算符](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators)上的wiki以查看余数运算符`%`
**有3个参数**
* currentElement
* 这是一个变量,表示传递给回调的元素。
* 指数
* 这是从0开始的当前元素的索引值
* 排列
* 调用`.some()`的数组。
回调函数应该实现一个测试用例。
## thisArg
是可选参数,可以在\[MDN\]找到更多信息
## 描述
`.some()`将为数组中的每个元素运行回调函数。一旦回调返回true `.some()`将返回`true` 。如果回调为数组中的_每个_元素返回一个[伪值](https://developer.mozilla.org/en-US/docs/Glossary/Falsy) ,则`.some()`返回false。
`.some()`不会改变/改变调用它的数组。
## 例子
**将函数传递给`.some()`**
```javascript
var isEven = function isEven(currentElement, index, array) {
if(currentElement % 2 === 0) {
return true;
} else {
return false;
}
}
var arr1 = [1, 2, 3, 4, 5, 6];
arr1.some(isEven); // returns true
var arr2 = [1, 3, 5, 7];
arr2.some(isEven); // returns false
```
**匿名功能**
```javascript
var arr3 = ['Free', 'Code', 'Camp', 'The Amazing'];
arr3.some(function(curr, index, arr) {
if (curr === 'The Amazing') {
return true;
}
}); // returns true
var arr4 = [1, 2, 14, 5, 17, 9];
arr4.some(function(curr, index, arr) {
return curr > 20;
}); // returns false
// ES6 arrows functions
arr4.some((curr) => curr >= 14) // returns true
```
资料来源: [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some)

View File

@@ -0,0 +1,51 @@
---
title: Array.prototype.sort
localeTitle: Array.prototype.sort
---
## Array.prototype.sort
此方法对数组中的元素进行排序并返回该数组。
`sort()`方法遵循**ASCII顺序**
指数|人物 --- | --- 33 | 34 |” 35 | 36 | $ 37 |
```js
var myArray = ['#', '!'];
var sortedArray = myArray.sort(); // ['!', '#'] because in the ASCII table "!" is before "#"
myArray = ['a', 'c', 'b'];
console.log(myArray.sort()); // ['a', 'b', 'c']
console.log(myArray) // ['a', 'b', 'c']
myArray = ['b', 'a', 'aa'];
console.log(myArray.sort()); // ['a', 'aa', 'b']
myArray = [1, 2, 13, 23];
console.log(myArray.sort()); // [1, 13, 2, 23] numbers are treated like strings!
```
# 高级用法
`sort()`方法也可以接受一个参数: `array.sort(compareFunction)`
### 例如
```js
function compare(a, b){
if (a < b){return -1;}
if (a > b){return 1;}
if (a === b){return 0;}
}
var myArray = [1, 2, 23, 13];
console.log(myArray.sort()); // [ 1, 13, 2, 23 ]
console.log(myArray.sort(compare)); // [ 1, 2, 13, 23 ]
myArray = [3, 4, 1, 2];
sortedArray = myArray.sort(function(a, b){.....}); // it depends from the compareFunction
```
#### 更多信息:
[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)

View File

@@ -0,0 +1,74 @@
---
title: Array.prototype.splice
localeTitle: Array.prototype.splice
---
## Array.prototype.splice
splice方法类似于[Array.prototype.slice](https://guide.freecodecamp.org/javascript/standard-objects/array/array-prototype-slice) ,但与`slice()`不同,它会改变调用它的数组。它的不同之处还在于它可以用于向数组添加值以及删除它们。
### 参数
`splice()`可以带一个或多个
#### 拼接(开始)
如果只包含一个参数,则`splice(start)`将从数组的`start`到结尾删除所有数组元素。
```js
let exampleArray = ['first', 'second', 'third', 'fourth'];
exampleArray.splice(2);
// exampleArray is now ['first', 'second'];
```
如果`start`为负数,它将从数组末尾向后计数。
```js
let exampleArray = ['first', 'second', 'third', 'fourth'];
exampleArray.splice(-1);
// exampleArray is now ['first', 'second', 'third'];
```
#### splicestartdeleteCount
如果包括第二参数,然后`splice(start, deleteCount)`将删除`deleteCount`从数组元素,开头`start`
```js
let exampleArray = ['first', 'second', 'third', 'fourth'];
exampleArray.splice(1, 2);
// exampleArray is now ['first', 'fourth'];
```
#### splicestartdeleteCountnewElement1newElement2...
如果包含两个以上的参数,则附加参数将是添加到阵列的新元素。这些添加元素的位置将`start`
通过传递`0`作为第二个参数,可以添加元素而不删除任何元素。
```js
let exampleArray = ['first', 'second', 'third', 'fourth'];
exampleArray.splice(1, 0, 'new 1', 'new 2');
// exampleArray is now ['first', 'new 1', 'new 2', 'second', 'third', 'fourth']
```
元素也可以替换。
```js
let exampleArray = ['first', 'second', 'third', 'fourth'];
exampleArray.splice(1, 2, 'new second', 'new third');
// exampleArray is now ['first', 'new second', 'new third', 'fourth']
```
### 返回值
除了更改调用它的数组外, `splice()`还返回一个包含已删除值的数组。这是一种将阵列切割成两个不同阵列的方法。
```js
let exampleArray = ['first', 'second', 'third', 'fourth'];
let newArray = exampleArray.splice(1, 2);
// exampleArray is now ['first', 'fourth']
// newArray is ['second', 'third']
```
#### 更多信息:
[MDN - Array.prototype.slice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice)

View File

@@ -0,0 +1,49 @@
---
title: Array.prototype.toLocaleString
localeTitle: Array.prototype.toLocaleString
---
## Array.prototype.toLocaleString
`toLocaleString()`方法返回表示数组元素的字符串。使用toLocaleString方法将所有元素转换为字符串。调用此函数的结果旨在特定于语言环境。
##### 句法:
```
arr.toLocaleString();
```
##### 参数
* `locales` (可选) - 包含字符串或语言标记数组的参数[BCP 47语言标记](http://tools.ietf.org/html/rfc5646) 。
* `options` (可选) - 具有配置属性的对象
##### 返回值
表示由特定于语言环境的String例如逗号“分隔的数组元素的字符串
## 例子
```javascript
var number = 12345;
var date = new Date();
var myArray = [number, date, 'foo'];
var myString = myArray.toLocaleString();
console.log(myString);
// OUTPUT '12345,10/25/2017, 4:20:02 PM,foo'
```
可以基于语言和区域标识符(区域设置)显示不同的输出。
```javascript
var number = 54321;
var date = new Date();
var myArray = [number, date, 'foo'];
var myJPString = myArray.toLocaleString('ja-JP');
console.log(myJPString);
// OUTPUT '54321,10/26/2017, 5:20:02 PM,foo'
```
### 更多信息:
资料来源: [MDN](https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Array/toLocaleString)

View File

@@ -0,0 +1,11 @@
---
title: Array.prototype.toSource
localeTitle: Array.prototype.toSource
---
## Array.prototype.toSource
这是一个存根。 [帮助我们的社区扩展它](https://github.com/freecodecamp/guides/tree/master/src/pages/javascript/standard-objects/array/array-prototype-tosource/index.md) 。
[这种快速风格指南有助于确保您的拉取请求被接受](https://github.com/freecodecamp/guides/blob/master/README.md) 。
#### 更多信息:

View File

@@ -0,0 +1,25 @@
---
title: Array.prototype.toString
localeTitle: Array.prototype.toString
---
JavaScript数组方法`.toString()`用于将数组转换为单个字符串,每个元素用逗号连接。该方法没有参数。
**句法**
```javascript
var arr = [1, 2, 3, 4];
arr.toString();
```
## 用法
```javascript
var str1 = [1, 2, 3, 4, 5].toString(); // str1 = '1,2,3,4,5';
var str2 = ['1', '2', '3', '4'].toString(); // str2 = '1,2,3,4';
var str3 = ['Free', 'Code', 'Camp'].toString(); // str3 = 'Free,Code,Camp';
var str4 = ['phone', '555-6726'].toString(); // str4 = 'phone,555-6726';
var str5 = ['August', 'September', 'October'].toString(); // str5 = 'August,September,October';
var str6 = ['Words', 'and', 3, 4].toString(); // str6 = 'Words,and,3,4';
```
资料来源: [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString)

View File

@@ -0,0 +1,34 @@
---
title: Array.prototype.unshift
localeTitle: Array.prototype.unshift
---
JavaScript数组方法`.unshift()`将一个或多个元素添加到数组的开头,并返回数组的新长度。
**句法**
```
arr.unshift([element1[, ...[, elementN]]])
```
## 参数
要添加到数组前面的元素。
## 返回
调用方法的数组的新`length`
## 例子
```
var array = [1, 2, 3, 4, 5];
array.unshift(0);
// If we console.log(array.shift()); the console would output 6.
// array is now [0, 1, 2, 3, 4, 5];
array.unshift([-1]);
// array is now [[-1], 0, 1, 2, 3, 4, 5];
```
![:rocket:](//forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=2 ":火箭:") [运行代码](https://repl.it/C2V3)
来源[MDN](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift)

View File

@@ -0,0 +1,38 @@
---
title: Array.prototype.values
localeTitle: Array.prototype.values
---
## Array.prototype.values
`values`方法返回一个新的`Array Iterator`对象,该对象包含数组中每个索引的值。
### 句法
```javascript
arr.values()
```
### 返回
一个新的`array` ittertator对象。
### 例
```javascript
let friends = ["Rachel", "Monica", "Chandler", "Phoebe", "Joey", "Ross"]
for (let friend of friends) {
console.log(friend)
}
// Rachel
// Monica
// Chandler
// Phoebe
// Joey
// Ross
```
#### 更多信息:
[MDN文档](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values)

View File

@@ -0,0 +1,11 @@
---
title: Array
localeTitle: 排列
---
## 排列
这是一个存根。 [帮助我们的社区扩展它](https://github.com/freecodecamp/guides/tree/master/src/pages/javascript/standard-objects/array/index.md) 。
[这种快速风格指南有助于确保您的拉取请求被接受](https://github.com/freecodecamp/guides/blob/master/README.md) 。
#### 更多信息: