2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 56bbb991ad1ed5201cd392cd
|
2021-03-15 03:20:39 +00:00
|
|
|
|
title: 使用 shift() 操作数组
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 1
|
2020-04-29 10:29:13 +00:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cRbVETW'
|
|
|
|
|
forumTopicId: 18238
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: manipulate-arrays-with-shift
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`pop()` 函数用来移出数组中最后一个元素。 如果想要移出第一个元素要怎么办呢?
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
这时候我们就需要 `.shift()` 了。 它的工作原理就像 `.pop()`,但它移除的是第一个元素,而不是最后一个。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
示例:
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
var ourArray = ["Stimpson", "J", ["cat"]];
|
|
|
|
|
var removedFromOurArray = ourArray.shift();
|
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
`removedFromOurArray` 值为 `Stimpson`,`ourArray` 值为 `["J", ["cat"]]`
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-15 03:20:39 +00:00
|
|
|
|
使用 `.shift()` 函数移除 `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` 应该只包含 `[["dog", 3]]`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert(
|
|
|
|
|
(function (d) {
|
|
|
|
|
if (d[0][0] == 'dog' && d[0][1] === 3 && 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
|
|
|
|
`removedFromMyArray` 应该包含 `["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] == 'John' &&
|
|
|
|
|
d[1] === 23 &&
|
|
|
|
|
typeof removedFromMyArray === 'object'
|
|
|
|
|
) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
})(removedFromMyArray)
|
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
(function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// Setup
|
|
|
|
|
var myArray = [["John", 23], ["dog", 3]];
|
|
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
var removedFromMyArray;
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
2020-04-29 10:29:13 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```js
|
|
|
|
|
var myArray = [["John", 23], ["dog", 3]];
|
|
|
|
|
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
var removedFromMyArray = myArray.shift();
|
|
|
|
|
```
|