[[๐Ÿ“—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 ้…ๅˆ—ใฎๆกไปถๅˆคๅฎš]]