1.3 KiB
1.3 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
587d7b7b367417b2b2512b17 | 组合使用数组和扩展运算符 | 1 | 301156 |
--description--
展开运算符的另一个大用处是合并数组,或者将某个数组的所有元素插入到另一个数组的任意位置。用传统的语法我们也可以连接两个数组,但只能两个数组首尾相接。而展开语法能使下面的操作变得极其简单:
let thisArray = ['sage', 'rosemary', 'parsley', 'thyme'];
let thatArray = ['basil', 'cilantro', ...thisArray, 'coriander'];
// thatArray 现在是 ['basil', 'cilantro', 'sage', 'rosemary', 'parsley', 'thyme', 'coriander']
使用展开语法,我们这样就实现了一个用传统方法要写得很复杂冗长的操作。
--instructions--
我们已经定义了一个返回sentence
变量的spreadOut
函数,请修改该函数,利用展开运算符使该函数返回数组['learning', 'to', 'code', 'is', 'fun']
。
--hints--
spreadOut
应该返回["learning", "to", "code", "is", "fun"]
assert.deepEqual(spreadOut(), ['learning', 'to', 'code', 'is', 'fun']);
spreadOut
函数里应该用到展开语法
assert.notStrictEqual(spreadOut.toString().search(/[...]/), -1);