concat方法作为一种将数组组合成新数组而不改变原始数组的方法。将concat与push方法进行比较。 Push将一个项添加到调用它的同一个数组的末尾,这会改变该数组。这是一个例子: var arr = [1,2,3];
arr.push([4,5,6]);
// arr更改为[1,2,3,[4,5,6]]
//不是函数式编程方式
Concat提供了一种在数组末尾添加新项目而无任何变异副作用的方法。 nonMutatingPush函数,使其使用concat将newItem添加到original结尾而不是push 。该函数应返回一个数组。 concat方法。
testString: 'assert(code.match(/\.concat/g), "Your code should use the concat method.");'
- text: 您的代码不应使用push方法。
testString: 'assert(!code.match(/\.push/g), "Your code should not use the push method.");'
- text: 第first数组不应该改变。
testString: 'assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]), "The first array should not change.");'
- text: second数组不应该改变。
testString: 'assert(JSON.stringify(second) === JSON.stringify([4, 5]), "The second array should not change.");'
- text: 'nonMutatingPush([1, 2, 3], [4, 5])应该返回[1, 2, 3, 4, 5] 。'
testString: 'assert(JSON.stringify(nonMutatingPush([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]), "nonMutatingPush([1, 2, 3], [4, 5]) should return [1, 2, 3, 4, 5].");'
```