59 lines
2.3 KiB
Markdown
59 lines
2.3 KiB
Markdown
|
---
|
|||
|
id: 587d7b7b367417b2b2512b17
|
|||
|
title: Combine Arrays with the Spread Operator
|
|||
|
challengeType: 1
|
|||
|
videoUrl: ''
|
|||
|
localeTitle: 将数组与Spread运算符组合在一起
|
|||
|
---
|
|||
|
|
|||
|
## Description
|
|||
|
<section id="description"> <dfn>扩展</dfn>运算符的另一个巨大优势是能够组合数组,或者在任何索引处将一个数组的所有元素插入到另一个数组中。使用更传统的语法,我们可以连接数组,但这只允许我们在一个数组的末尾和另一个数组的开头组合数组。 Spread语法使以下操作非常简单: <blockquote>让thisArray = ['sage','迷迭香','欧芹','百里香']; <br><br>让那个阵容= ['罗勒','香菜',......这个阿雷,'香菜']; <br> //现在等于['罗勒','香菜','鼠尾草','迷迭香','欧芹','百里香','香菜'] </blockquote>使用扩展语法,我们刚刚实现了一个操作,如果我们使用传统方法,这个操作会更复杂,更冗长。 </section>
|
|||
|
|
|||
|
## Instructions
|
|||
|
<section id="instructions">我们定义了一个函数<code>spreadOut</code> ,它返回变量<code>sentence</code> ,使用<dfn>spread</dfn>运算符修改函数,使它返回数组<code>['learning', 'to', 'code', 'is', 'fun']</code> 。 </section>
|
|||
|
|
|||
|
## Tests
|
|||
|
<section id='tests'>
|
|||
|
|
|||
|
```yml
|
|||
|
tests:
|
|||
|
- text: '<code>spreadOut</code>应该返回<code>["learning", "to", "code", "is", "fun"]</code>'
|
|||
|
testString: 'assert.deepEqual(spreadOut(), ["learning", "to", "code", "is", "fun"], "<code>spreadOut</code> should return <code>["learning", "to", "code", "is", "fun"]</code>");'
|
|||
|
- text: <code>spreadOut</code>函数应该使用扩展语法
|
|||
|
testString: 'assert.notStrictEqual(spreadOut.toString().search(/[...]/), -1, "The <code>spreadOut</code> function should utilize spread syntax");'
|
|||
|
|
|||
|
```
|
|||
|
|
|||
|
</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
|
|||
|
```
|
|||
|
</section>
|