--- id: 587d78b2367417b2b2512b10 title: Remove Items Using splice() challengeType: 1 videoUrl: '' localeTitle: 使用splice()删除项目 --- ## Description
好的,所以我们已经学会了如何使用shift()pop()从数组的开头和结尾删除元素,但是如果我们想要从中间某处删除元素呢?或者一次删除多个元素?好吧,这就是splice()用武之地splice()允许我们这样做:从数组中的任何位置删除任意数量的连续元素splice()可能需要长达3个参数,但现在,我们将重点放在刚第一2.第2个参数splice()是其代表索引的整数,或位置中,阵列的该splice()是为呼吁。请记住,数组是零索引的 ,所以为了表示数组的第一个元素,我们将使用0splice()的第一个参数表示从中开始删除元素的数组的索引,而第二个参数表示要删除的元素的数量。例如:
让array = ['今天','是','不','所以','伟大'];

array.splice(2,2);
//删除以第3个元素开头的2个元素
//数组现在等于['今天','是','很棒']
splice()不仅修改了它被调用的数组,而且还返回一个包含被删除元素值的新数组:
让array = ['我','我','感觉','真的','快乐'];

let newArray = array.splice(3,2);
// newArray等于['真'','快乐']
## Instructions
我们定义了一个函数sumOfTen ,它将一个数组作为参数,并返回该数组元素的总和。使用splice()修改函数,使其返回值10
## Tests
```yml tests: - text: sumOfTen应该返回10 testString: 'assert.strictEqual(sumOfTen([2, 5, 1, 5, 2, 1]), 10, "sumOfTen should return 10");' - text: sumOfTen函数应该使用splice()方法 testString: 'assert.notStrictEqual(sumOfTen.toString().search(/\.splice\(/), -1, "The sumOfTen function should utilize the splice() method");' ```
## Challenge Seed
```js function sumOfTen(arr) { // change code below this line // change code above this line return arr.reduce((a, b) => a + b); } // do not change code below this line console.log(sumOfTen([2, 5, 1, 5, 2, 1])); ```
## Solution
```js // solution required ```