2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: 56bbb991ad1ed5201cd392cc
|
2021-03-15 03:20:39 +00:00
|
|
|
title: 使用 pop() 操作数组
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 1
|
2020-04-29 10:29:13 +00:00
|
|
|
videoUrl: 'https://scrimba.com/c/cRbVZAB'
|
|
|
|
forumTopicId: 18236
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: manipulate-arrays-with-pop
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
改变数组中数据的另一种方法是用 `.pop()` 函数。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`.pop()` 函数用来弹出一个数组末尾的值。 我们可以把这个弹出的值赋给一个变量存储起来。 换句话说就是 `.pop()` 函数移除数组末尾的元素并返回这个元素。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
数组中任何类型的元素(数值,字符串,甚至是数组)都可以被弹出来 。
|
2020-04-29 10:29:13 +00:00
|
|
|
|
|
|
|
```js
|
2021-11-06 15:56:52 +00:00
|
|
|
const threeArr = [1, 4, 6];
|
|
|
|
const oneDown = threeArr.pop();
|
2021-03-15 03:20:39 +00:00
|
|
|
console.log(oneDown);
|
|
|
|
console.log(threeArr);
|
2020-04-29 10:29:13 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
第一个 `console.log` 将显示值 `6`,第二个将显示值 `[1, 4]`。
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-11-06 15:56:52 +00:00
|
|
|
使用 `.pop()` 函数从 `myArray` 中删除最后一项,并将取出的值分配给新变量 `removedFromMyArray`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`myArray` 应该只包含 `[["John", 23]]`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(
|
|
|
|
(function (d) {
|
|
|
|
if (d[0][0] == 'John' && d[0][1] === 23 && d[1] == undefined) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
})(myArray)
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
对 `myArray` 使用 `pop()` 函数。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(/removedFromMyArray\s*=\s*myArray\s*.\s*pop\s*(\s*)/.test(code));
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
`removedFromMyArray` 应该只包含 `["cat", 2]`。
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2018-10-10 22:03:03 +00:00
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
assert(
|
|
|
|
(function (d) {
|
|
|
|
if (d[0] == 'cat' && d[1] === 2 && d[2] == undefined) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
})(removedFromMyArray)
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
2020-04-29 10:29:13 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
```js
|
2021-11-06 15:56:52 +00:00
|
|
|
if (typeof removedFromMyArray !== 'undefined') (function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
|
2021-01-13 02:31:00 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Setup
|
2021-11-06 15:56:52 +00:00
|
|
|
const myArray = [["John", 23], ["cat", 2]];
|
2021-01-13 02:31:00 +00:00
|
|
|
|
|
|
|
// Only change code below this line
|
2021-11-06 15:56:52 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```js
|
2021-11-06 15:56:52 +00:00
|
|
|
const myArray = [["John", 23], ["cat", 2]];
|
|
|
|
const removedFromMyArray = myArray.pop();
|
2021-01-13 02:31:00 +00:00
|
|
|
```
|