[[๐TDQ-016 ้
ๅใฎๅคๆ ใใฎ๏ผ]]ใฎ็ถใใงใใๅๅใใใๅฐใใฏใปใฎใใใกใฝใใใๆฑใใพใใ
## Reference
- [้
ๅ ยท JavaScript Primer \#jsprimer](https://jsprimer.net/basic/array/)
## Lesson
### concat
[[้
ๅ (JavaScript)|้
ๅ]]ๅๅฃซใฎ็ตๅใซใฏ[[Array.prototype.concat (JavaScript)|Array.prototype.concat]]ใไฝฟใใพใใ
```js
const xs = [1, 2];
const ys = [3, 4, 5];
xs.concat(ys);
// [ 1, 2, 3, 4, 5 ]
xs.concat(ys, ys);
// [ 1, 2, 3, 4, 5, 3, 4, 5 ]
```
### toSorted
[[้
ๅ (JavaScript)|้
ๅ]]ใฎใฝใผใใซใฏ[[Array.prototype.toSorted (JavaScript)|Array.prototype.toSorted]]ใไฝฟใใพใใ
```js
["c", "d", "a", "b"].toSorted()
// [ "a", "b", "c", "d" ]
```
### toReversed
[[้
ๅ (JavaScript)|้
ๅ]]ใฎ้ ็ช้่ปขใซใฏ[[Array.prototype.toReversed (JavaScript)|Array.prototype.toReversed]]ใไฝฟใใพใใ
```js
["a", "b", "c", "d"].toReversed()
// [ "d", "c", "b", "a" ]
```
### toSpliced
[[้
ๅ (JavaScript)|้
ๅ]]ใฎไธ้จ่ฆ็ด ใๆฟๅ
ฅใใใใๅ้คใใใใใใซใฏ[[Array.prototype.toSpliced (JavaScript)|Array.prototype.toSpliced]]ใไฝฟใใพใใ
```js
["1", "2", "3", "4"].toSpliced(2, 0, "new") // ๆฟๅ
ฅ
// ["1", "2", "new", "3", "4"]
["1", "2", "3", "4"].toSpliced(1, 2) // ๅ้ค
// ["1", "4"]
```
### reduce
[[้
ๅ (JavaScript)|้
ๅ]]ใฎๅคใ1ใคใซ็ณใฟ่พผใใซใฏ[[Array.prototype.reduce (JavaScript)|Array.prototype.reduce]]ใไฝฟใใพใใ
```js
[1, 10, 100].reduce((acc, x) => acc + x) // ๅๆๅคใ้
ๅ1ใค็ฎใฎ่ฆ็ด
// 111
[1, 10, 100].reduce((acc, x) => acc + x, 100) // ๅๆๅคใๆๅฎๅค(100)
// 211
```
## Mission 1
#๐EASY
[[Array.prototype.reduce (JavaScript)|Array.prototype.reduce]]ใไฝฟใฃใฆ1ใใ5ใฎๅใๆฑใใใณใผใใๆธใใฆใใ ใใใ
%%
่งฃ็ญไพ
```js
[1, 2, 3, 4, 5].reduce((acc, x) => acc + x)
```
%%
## Mission 2
#๐NORMAL
ไปฅไธใฎใณใผใใ[[Array.prototype.flat (JavaScript)|Array.prototype.flat]]ใไฝฟใใใซใ[[Array.prototype.concat (JavaScript)|Array.prototype.concat]]ใจ[[Array.prototype.reduce (JavaScript)|Array.prototype.reduce]]ใไฝฟใฃใฆๅฎ่ฃ
ใใชใใใฆใใ ใใใ
```js
const box = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
box.flat();
// [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
```
%%
่งฃ็ญไพ
```js
const box = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
];
box.reduce((acc, xs) => acc.concat(xs));
```
%%
> [!hint]- Hint 1
> [[#Mission 1]]ใจๅบๆฌ็ใชๆตใใฏๅใใงใใ
## Mission 3
#๐NORMAL
ไปฅไธใฎใในใใๅคฑๆใใ็็ฑใ่ชฌๆใใฆใใ ใใใ
```js
import { expect } from "jsr:@std/expect";
Deno.test("ๅ
ใฎ้
ๅใฎๆๅพใฎ่ฆ็ด ใฏ้่ปข้
ๅใฎๅ
้ ญใจ็ญใใ", () => {
const xs = [1, 2, 3, 4, 5];
const reversedXs = xs.reverse();
expect(reversedXs.at(0)).toEqual(xs.at(-1));
});
```
%%
่งฃ็ญไพ
[[Array.prototype.reverse (JavaScript)|Array.prototype.reverse]]ใฏๅ
ใฎ[[้
ๅ (JavaScript)|้
ๅ]]ใ็ ดๅฃ็ใซๅคๆดใใฆใใพใ (ๅ
ใฎ้
ๅใฎ้ ็ชใ้่ปขใใฆใใพใ) ใใใ
%%
## Mission 4
#๐NORMAL
[[#Mission 3]]ใฎใณใผใใใในใใ้ใใใใซ**1่กใ ใ**ไฟฎๆญฃใใฆใใ ใใใ
%%
่งฃ็ญไพ
```js
import { expect } from "jsr:@std/expect";
Deno.test("ๅ
ใฎ้
ๅใฎๆๅพใฎ่ฆ็ด ใฏ้่ปข้
ๅใฎๅ
้ ญใจ็ญใใ", () => {
const xs = [1, 2, 3, 4, 5];
const reversedXs = xs.toReversed();
expect(reversedXs.at(0)).toEqual(xs.at(-1));
});
```
%%
## Mission 5
#๐NORMAL
ไปฅไธใฎใณใผใใFIXMEใณใกใณใใฎๅถ็ดใซๅพใฃใฆๅฎๆใใใฆใใ ใใใ
```js
import { expect } from "jsr:@std/expect";
Deno.test("3็ช็ฎใฎ่ฆ็ด ใ้คๅคใใฆใใๆๅญๆฐใใญใผใจใใฆใฐใซใผใใณใฐใใ", () => {
const xs = ["one", "two", "three", "four", "five"];
// FIXME: xxxใจyyyใซ้ฉๅใชใกใฝใใใๆๅฎใใ
const r = Object.xxx(xs.yyy(2, 1), (x) => x.length);
expect(r).toEqual({ 3: ["one", "two"], 4: ["four", "five"] });
expect(xs).toEqual(["one", "two", "three", "four", "five"]);
});
```
%%
่งฃ็ญไพ
```js
import { expect } from "jsr:@std/expect";
Deno.test("3็ช็ฎใฎ่ฆ็ด ใ้คๅคใใฆใใๆๅญๆฐใใญใผใจใใฆใฐใซใผใใณใฐใใ", () => {
const xs = ["one", "two", "three", "four", "five"];
const r = Object.groupBy(xs.toSpliced(2, 1), (x) => x.length);
expect(r).toEqual({ 3: ["one", "two"], 4: ["four", "five"] });
expect(xs).toEqual(["one", "two", "three", "four", "five"]);
});
```
%%
> [!hint]- Hint 1
> ่ฆ็ด ใฎๅ้คใซใฏ[[#toSpliced]]ใไฝฟใใพใใ
---
*NEXT* >> [[๐TDQ-018 ้
ๅใฎๆกไปถๅคๅฎ]]