2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7da9367417b2b2512b66
|
2021-03-15 14:01:16 -06:00
|
|
|
|
title: 使用 concat 方法组合两个数组
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 1
|
2020-08-05 16:38:04 +08:00
|
|
|
|
forumTopicId: 301229
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: combine-two-arrays-using-the-concat-method
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-03-15 14:01:16 -06:00
|
|
|
|
<dfn>Concatenation</dfn> 意思是将元素连接到尾部。 同理,JavaScript 为字符串和数组提供了`concat`方法。 对数组来说,在一个数组上调用 `concat` 方法,然后提供另一个数组作为参数添加到第一个数组末尾。 它返回一个新数组,不会改变任何一个原始数组。 举个例子:
|
2020-08-05 16:38:04 +08:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
[1, 2, 3].concat([4, 5, 6]);
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 14:01:16 -06:00
|
|
|
|
返回的数组将是 `[1, 2, 3, 4, 5, 6]`。
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-15 14:01:16 -06:00
|
|
|
|
在 `nonMutatingConcat` 函数里使用 `concat`,将 `attach` 拼接到 `original` 尾部。 函数返回拼接后的数组。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-15 14:01:16 -06:00
|
|
|
|
应该使用 `concat` 方法。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/\.concat/g));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 14:01:16 -06:00
|
|
|
|
不应该改变 `first` 数组。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-15 14:01:16 -06:00
|
|
|
|
不应该改变 `second` 数组。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(JSON.stringify(second) === JSON.stringify([4, 5]));
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-15 14:01:16 -06:00
|
|
|
|
`nonMutatingConcat([1, 2, 3], [4, 5])` 应该返回 `[1, 2, 3, 4, 5]`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
|
assert(
|
|
|
|
|
JSON.stringify(nonMutatingConcat([1, 2, 3], [4, 5])) ===
|
|
|
|
|
JSON.stringify([1, 2, 3, 4, 5])
|
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-05 16:38:04 +08:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function nonMutatingConcat(original, attach) {
|
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Only change code above this line
|
|
|
|
|
}
|
2021-10-27 15:10:57 +00:00
|
|
|
|
|
|
|
|
|
const first = [1, 2, 3];
|
|
|
|
|
const second = [4, 5];
|
2021-01-13 03:31:00 +01:00
|
|
|
|
nonMutatingConcat(first, second);
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```js
|
|
|
|
|
function nonMutatingConcat(original, attach) {
|
|
|
|
|
return original.concat(attach);
|
|
|
|
|
}
|
2021-10-27 15:10:57 +00:00
|
|
|
|
const first = [1, 2, 3];
|
|
|
|
|
const second = [4, 5];
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```
|