2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7b7b367417b2b2512b17
|
|
|
|
challengeType: 1
|
2020-08-04 15:14:21 +08:00
|
|
|
forumTopicId: 301156
|
2020-10-01 17:54:21 +02:00
|
|
|
title: 组合使用数组和扩展运算符
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-08-04 15:14:21 +08:00
|
|
|
<section id='description'>
|
|
|
|
<dfn>展开运算符</dfn>的另一个大用处是合并数组,或者将某个数组的所有元素插入到另一个数组的任意位置。用传统的语法我们也可以连接两个数组,但只能两个数组首尾相接。而展开语法能使下面的操作变得极其简单:
|
|
|
|
|
|
|
|
```js
|
|
|
|
let thisArray = ['sage', 'rosemary', 'parsley', 'thyme'];
|
|
|
|
|
|
|
|
let thatArray = ['basil', 'cilantro', ...thisArray, 'coriander'];
|
|
|
|
// thatArray 现在是 ['basil', 'cilantro', 'sage', 'rosemary', 'parsley', 'thyme', 'coriander']
|
|
|
|
```
|
|
|
|
|
|
|
|
使用展开语法,我们这样就实现了一个用传统方法要写得很复杂冗长的操作。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-08-04 15:14:21 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
我们已经定义了一个返回<code>sentence</code>变量的<code>spreadOut</code>函数,请修改该函数,利用<dfn>展开运算符</dfn>使该函数返回数组<code>['learning', 'to', 'code', 'is', 'fun']</code>。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
|
|
|
- text: '<code>spreadOut</code>应该返回<code>["learning", "to", "code", "is", "fun"]</code>'
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(spreadOut(), ['learning', 'to', 'code', 'is', 'fun']);
|
2020-08-04 15:14:21 +08:00
|
|
|
- text: <code>spreadOut</code>函数里应该用到展开语法
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.notStrictEqual(spreadOut.toString().search(/[...]/), -1);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
function spreadOut() {
|
|
|
|
let fragment = ['to', 'code'];
|
|
|
|
let sentence; // change this line
|
|
|
|
return sentence;
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not change code below this line
|
|
|
|
console.log(spreadOut());
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
// solution required
|
2020-08-04 15:14:21 +08:00
|
|
|
|
|
|
|
function spreadOut() {
|
|
|
|
let fragment = ['to', 'code'];
|
|
|
|
let sentence = ['learning', ...fragment, 'is', 'fun'];
|
|
|
|
return sentence;
|
|
|
|
}
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-04 15:14:21 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|