2021-05-05 10:13:49 -07:00
|
|
|
|
---
|
|
|
|
|
id: 56bbb991ad1ed5201cd392cd
|
|
|
|
|
title: 使用 shift() 操作數組
|
|
|
|
|
challengeType: 1
|
|
|
|
|
videoUrl: 'https://scrimba.com/c/cRbVETW'
|
|
|
|
|
forumTopicId: 18238
|
|
|
|
|
dashedName: manipulate-arrays-with-shift
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
|
|
|
|
|
`pop()` 函數用來移出數組中最後一個元素。 如果想要移出第一個元素要怎麼辦呢?
|
|
|
|
|
|
|
|
|
|
這時候我們就需要 `.shift()` 了。 它的工作原理就像 `.pop()`,但它移除的是第一個元素,而不是最後一個。
|
|
|
|
|
|
|
|
|
|
示例:
|
|
|
|
|
|
|
|
|
|
```js
|
2021-11-06 08:56:52 -07:00
|
|
|
|
const ourArray = ["Stimpson", "J", ["cat"]];
|
|
|
|
|
const removedFromOurArray = ourArray.shift();
|
2021-05-05 10:13:49 -07:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`removedFromOurArray` 值爲 `Stimpson`,`ourArray` 值爲 `["J", ["cat"]]`
|
|
|
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
|
2021-11-06 08:56:52 -07:00
|
|
|
|
使用 `.shift()` 函數從 `myArray` 中刪除第一項,並將“移除的值”值分配給新變量 `removedFromMyArray`。
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
|
|
|
|
`myArray` 應該只包含 `[["dog", 3]]`。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function (d) {
|
|
|
|
|
if (d[0][0] == 'dog' && d[0][1] === 3 && d[1] == undefined) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
})(myArray)
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`removedFromMyArray` 應該包含 `["John", 23]`。
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
(function (d) {
|
|
|
|
|
if (
|
|
|
|
|
d[0] == 'John' &&
|
|
|
|
|
d[1] === 23 &&
|
|
|
|
|
typeof removedFromMyArray === 'object'
|
|
|
|
|
) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
})(removedFromMyArray)
|
|
|
|
|
);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
|
|
```js
|
2021-11-06 08:56:52 -07:00
|
|
|
|
if (typeof removedFromMyArray !== 'undefined') (function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
|
2021-05-05 10:13:49 -07:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// Setup
|
2021-11-06 08:56:52 -07:00
|
|
|
|
const myArray = [["John", 23], ["dog", 3]];
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
// Only change code below this line
|
2021-11-06 08:56:52 -07:00
|
|
|
|
|
2021-05-05 10:13:49 -07:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
|
|
|
|
```js
|
2021-11-06 08:56:52 -07:00
|
|
|
|
const myArray = [["John", 23], ["dog", 3]];
|
2021-05-05 10:13:49 -07:00
|
|
|
|
|
|
|
|
|
// Only change code below this line
|
2021-11-06 08:56:52 -07:00
|
|
|
|
const removedFromMyArray = myArray.shift();
|
2021-05-05 10:13:49 -07:00
|
|
|
|
```
|