freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-data-structures/combine-arrays-with-the-spr...

2.3 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7b7b367417b2b2512b17 Combine Arrays with the Spread Operator 1 将数组与Spread运算符组合在一起

Description

扩展运算符的另一个巨大优势是能够组合数组,或者在任何索引处将一个数组的所有元素插入到另一个数组中。使用更传统的语法,我们可以连接数组,但这只允许我们在一个数组的末尾和另一个数组的开头组合数组。 Spread语法使以下操作非常简单
让thisArray = ['sage''迷迭香''欧芹''百里香'];

让那个阵容= ['罗勒''香菜'......这个阿雷,'香菜'];
//现在等于['罗勒''香菜''鼠尾草''迷迭香''欧芹''百里香''香菜']
使用扩展语法,我们刚刚实现了一个操作,如果我们使用传统方法,这个操作会更复杂,更冗长。

Instructions

我们定义了一个函数spreadOut ,它返回变量sentence ,使用spread运算符修改函数,使它返回数组['learning', 'to', 'code', 'is', 'fun']

Tests

tests:
  - text: '<code>spreadOut</code>应该返回<code>[&quot;learning&quot;, &quot;to&quot;, &quot;code&quot;, &quot;is&quot;, &quot;fun&quot;]</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");'

Challenge Seed

function spreadOut() {
  let fragment = ['to', 'code'];
  let sentence; // change this line
  return sentence;
}

// do not change code below this line
console.log(spreadOut());

Solution

// solution required