Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

83 lines
2.4 KiB
Markdown

---
id: 587d78b2367417b2b2512b10
title: 使用 splice() 删除元素
challengeType: 1
forumTopicId: 301166
dashedName: remove-items-using-splice
---
# --description--
在之前的挑战中,我们已经学习了如何用 `shift()``pop()` 从数组的开头或末尾移除元素。但如果我们想删除数组中间的一个元素,或者想一次删除多个元素,该如何操作呢?这时候我们就需要使用 `splice()` 方法了,`splice()` 可以让我们从数组中的任意位置**连续删除任意数量的元素**。
`splice()` 最多可以接受 3 个参数,但现在我们先关注前两个。`splice()` 接收的前两个参数以调用 `splice()` 数组中的元素索引作为参考。别忘了,数组的索引是*从 0 开始的*,所以我们要用 `0` 来表示数组中的第一个元素。`splice()` 的第一个参数代表从数组中的哪个索引开始移除元素,而第二个参数表示要从数组中的这位位置删除多少个元素。例如:
```js
let array = ['today', 'was', 'not', 'so', 'great'];
array.splice(2, 2);
// 从索引为 2 的位置(即第三个元素)开始移除 2 个元素
// array 的值现在是 ['today', 'was', 'great']
```
`splice()` 不仅会修改调用该方法的数组,还会返回一个包含被移除元素的数组:
```js
let array = ['I', 'am', 'feeling', 'really', 'happy'];
let newArray = array.splice(3, 2);
// newArray 的值是 ['really', 'happy']
```
# --instructions--
我们已经定义了数组 `arr`。请使用 `splice()``arr` 里移除元素,使剩余的元素之和为 `10`
# --hints--
不应修改这一行 `const arr = [2, 4, 5, 1, 7, 5, 2, 1];`
```js
assert(code.replace(/\s/g, '').match(/constarr=\[2,4,5,1,7,5,2,1\];?/));
```
`ahr` 的剩余元素之和应为 `10`
```js
assert.strictEqual(
arr.reduce((a, b) => a + b),
10
);
```
应对 `arr` 调用 `splice()` 方法。
```js
assert(code.replace(/\s/g, '').match(/arr\.splice\(/));
```
splice 应只删除 `arr` 里面的元素,不能给 `arr` 添加元素。
```js
assert(!code.replace(/\s/g, '').match(/arr\.splice\(\d+,\d+,\d+.*\)/g));
```
# --seed--
## --seed-contents--
```js
const arr = [2, 4, 5, 1, 7, 5, 2, 1];
// Only change code below this line
// Only change code above this line
console.log(arr);
```
# --solutions--
```js
const arr = [2, 4, 5, 1, 7, 5, 2, 1];
arr.splice(1, 4);
```