slice()
允许我们选择要复制的数组元素,但在其他几个有用的任务中,ES6的新扩展运算符允许我们使用简单且高度可读的语法轻松地按顺序复制所有数组的元素。扩展语法看起来像这样: ...
在实践中,我们可以使用spread运算符来复制数组,如下所示: let thisArray = [true,true,undefined,false,null];
让thatArray = [... thisArray];
// thatArray等于[true,true,undefined,false,null]
// thisArray保持不变,与thatArray相同
copyMachine
,它将arr
(数组)和num
(数字)作为参数。该函数应该返回一个由arr
的num
副本组成的新数组。我们为您完成了大部分工作,但它还没有正常工作。使用扩展语法修改函数以使其正常工作(提示:我们已经介绍过的另一种方法可能会派上用场!)。 copyMachine([true, false, true], 2)
应返回[[true, false, true], [true, false, true]]
'
testString: 'assert.deepEqual(copyMachine([true, false, true], 2), [[true, false, true], [true, false, true]], "copyMachine([true, false, true], 2)
should return [[true, false, true], [true, false, true]]
");'
- text: 'copyMachine([1, 2, 3], 5)
应返回[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
'
testString: 'assert.deepEqual(copyMachine([1, 2, 3], 5), [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]], "copyMachine([1, 2, 3], 5)
should return [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
");'
- text: 'copyMachine([true, true, null], 1)
应该返回[[true, true, null]]
'
testString: 'assert.deepEqual(copyMachine([true, true, null], 1), [[true, true, null]], "copyMachine([true, true, null], 1)
should return [[true, true, null]]
");'
- text: 'copyMachine(["it works"], 3)
应该返回[["it works"], ["it works"], ["it works"]]
'
testString: 'assert.deepEqual(copyMachine(["it works"], 3), [["it works"], ["it works"], ["it works"]], "copyMachine(["it works"], 3)
should return [["it works"], ["it works"], ["it works"]]
");'
- text: copyMachine
函数应该使用带有数组arr
的spread operator
testString: 'assert.notStrictEqual(copyMachine.toString().indexOf(".concat(_toConsumableArray(arr))"), -1, "The copyMachine
function should utilize the spread operator
with array arr
");'
```