更優雅的編寫JavaScript

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你剛接觸JavaScript可能你還沒有聽說過"},{"type":"codeinline","content":[{"type":"text","text":".map()"}]},{"type":"text","text":","},{"type":"codeinline","content":[{"type":"text","text":".reduce()"}]},{"type":"text","text":","},{"type":"codeinline","content":[{"type":"text","text":".filter()"}]},{"type":"text","text":"。或者聽說過,看過別人用過但是自己在實際項目中沒有用過。在國內很多開發項目都是需要考慮IE8的兼容,爲了兼容很多JavaScript好用的方法和技巧都被埋沒了。但是我發現近幾年開始,很多開發項目已經完全拋棄了IE這個魔鬼了。如果你不需要兼容“石器時代”的IE瀏覽器了,那就要開始熟悉一下這幾個方法來處理數組。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意這遍文章說的的3個方法其實在很多其他語言都可以使用到,因爲這幾個方法和使用概念在很多其他語言都是存在的。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":".map()"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"讓我用一個簡單的例子告訴你如何使用這個方法。假如你現在有多對象的數組數據 - 每一個對象代表着一個員工的信息。現在你想要的最終結果就是取出所有員工的唯一ID值。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"// 員工數據\nvar employees = [\n { id: 20, name: 'Captain Piett' },\n { id: 24, name: 'General Veers' },\n { id: 56, name: 'Admiral Ozzel' },\n { id: 88, name: 'Commander Jerjerrod' }\n];\n// 你想要的結果\n[20, 24, 56, 88]"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其實要實現這個結果有很多數組處理方式。傳統的處理方法就是先定義一個空數組,然後使用"},{"type":"codeinline","content":[{"type":"text","text":".forEach()"}]},{"type":"text","text":","},{"type":"codeinline","content":[{"type":"text","text":".for(...of)"}]},{"type":"text","text":",或者是最簡單的"},{"type":"codeinline","content":[{"type":"text","text":".for()"}]},{"type":"text","text":"來組裝ID到你定義的數組裏面。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們來對比一下傳統的處理方式和"},{"type":"codeinline","content":[{"type":"text","text":".map()"}]},{"type":"text","text":"的區別。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用"},{"type":"codeinline","content":[{"type":"text","text":".forEach()"}]},{"type":"text","text":":"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"var employeeIds = [];\nemployees.forEach(function (employee) {\n employeeIds.push(officer.id);\n});"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意使用傳統的方式,我們必須有一個預定義的空數組變量纔行。但是如果是"},{"type":"codeinline","content":[{"type":"text","text":".map()"}]},{"type":"text","text":"就會更簡單了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"var employeeIds = employees.map(function (employee) {\n return employee.id\n});"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"甚至我們可以用更簡潔的方式,使用箭頭方法(但是需要ES6支持,Babel,或者TypeScript)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const employeeIds = employees.map(employee => employee.id);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以"},{"type":"codeinline","content":[{"type":"text","text":".map()"}]},{"type":"text","text":"到底是怎麼運作的呢?這個方法有兩個參數,第一是回調方法,第二是可選內容(會在回調方法中做爲"},{"type":"codeinline","content":[{"type":"text","text":"this"}]},{"type":"text","text":")。數組裏的"},{"type":"codeinline","content":[{"type":"text","text":"每個數值/對象會被循環進入到回調方法"}]},{"type":"text","text":"裏面,然後"},{"type":"codeinline","content":[{"type":"text","text":"返回新的數值/對象"}]},{"type":"text","text":"到結果數組裏面。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意結果數組的長度永遠都會和被循環的數組的長度一致。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":".reduce()"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"與"},{"type":"codeinline","content":[{"type":"text","text":".map()"}]},{"type":"text","text":"相識,"},{"type":"codeinline","content":[{"type":"text","text":".reduce()"}]},{"type":"text","text":"也是循環一個回調方法,數組裏面的每一個元素對回進入回調方法。區別是回調方法返回的值會被傳遞到下一個回調方法,如此類推(等同於一個累加器)。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":".reduce()"}]},{"type":"text","text":"裏的累加值可以是任何屬性的值,包括"},{"type":"codeinline","content":[{"type":"text","text":"integer"}]},{"type":"text","text":","},{"type":"codeinline","content":[{"type":"text","text":"string"}]},{"type":"text","text":","},{"type":"codeinline","content":[{"type":"text","text":"object"}]},{"type":"text","text":"等等。這個累加值會被實力化或者傳遞到下一個回調方法。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"來上代碼,做個簡單的例子!假如你有一個飛機師的數組,數組裏面有每個飛機師的工齡。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"var pilots = [\n {\n id: 10,\n name: \"Poe Dameron\",\n years: 14,\n },\n {\n id: 2,\n name: \"Temmin 'Snap' Wexley\",\n years: 30,\n },\n {\n id: 41,\n name: \"Tallissan Lintra\",\n years: 16,\n },\n {\n id: 99,\n name: \"Ello Asty\",\n years: 22,\n }\n];"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"現在我們需要知道所有飛機師累計的總工齡。使用"},{"type":"codeinline","content":[{"type":"text","text":".reduce()"}]},{"type":"text","text":"就是比喫飯還簡單的事情。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"var totalYears = pilots.reduce(function (accumulator, pilot) {\n return accumulator + pilot.years;\n}, 0);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"注意我這裏第二個參數我傳了0。第二個參數是一個累加值的初始值。當然如果場景需要這個初始值也可以傳入一個變量或者你需要的值。循環了數組裏的每一個元素後,reduce方法會返回最終累加後的值(在我們這個例子中就是"},{"type":"codeinline","content":[{"type":"text","text":"82"}]},{"type":"text","text":")。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例子裏面的"},{"type":"codeinline","content":[{"type":"text","text":"acc"}]},{"type":"text","text":"和"},{"type":"codeinline","content":[{"type":"text","text":"accumulator"}]},{"type":"text","text":"就是累加值變量"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果是使用ES6箭頭寫法,我們可以寫的更加優雅簡潔。一行就可以搞掂的事情!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const totalYears = pilots.reduce((acc, pilot) => acc + pilot.years, 0);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"現在如果我們需要找到哪一位是最有經驗的飛機師。這種情況我們一樣可以使用"},{"type":"codeinline","content":[{"type":"text","text":".reduce()"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"var mostExpPilot = pilots.reduce(function (oldest, pilot) {\n return (oldest.years || 0) > pilot.years ? oldest : pilot;\n}, {});"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏我把"},{"type":"codeinline","content":[{"type":"text","text":"accumulator"}]},{"type":"text","text":"變量改爲"},{"type":"codeinline","content":[{"type":"text","text":"oldest"}]},{"type":"text","text":"代表飛機師裏面的老司機。這時候reduce裏面的回調方法對比每一個飛機師,每一次飛機師的值進入這個回調方法,工齡更高的就會覆蓋"},{"type":"codeinline","content":[{"type":"text","text":"oldest"}]},{"type":"text","text":"變量。最終循環後得到的"},{"type":"codeinline","content":[{"type":"text","text":"oldest"}]},{"type":"text","text":"就是工齡最高的飛機師。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通過這幾個例子,你可以看到使用"},{"type":"codeinline","content":[{"type":"text","text":".reduce()"}]},{"type":"text","text":"可以簡單又優雅的在一個數組裏面獲取到單個最終值或者對象。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":".filter()"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你現在的場景是需要在一個數組裏面過濾一部分的數據,這個時候"},{"type":"codeinline","content":[{"type":"text","text":".filter()"}]},{"type":"text","text":"就是你的最好的朋友了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們用回飛機師的數據,並且加入了所屬航空公司的值:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"var pilots = [\n {\n id: 2,\n name: \"Wedge Antilles\",\n faction: \"Rebels\",\n },\n {\n id: 8,\n name: \"Ciena Ree\",\n faction: \"Empire\",\n },\n {\n id: 40,\n name: \"Iden Versio\",\n faction: \"Empire\",\n },\n {\n id: 66,\n name: \"Thane Kyrell\",\n faction: \"Rebels\",\n }\n];"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"加入現在我們想分別篩選出"},{"type":"codeinline","content":[{"type":"text","text":"Rebels"}]},{"type":"text","text":"和"},{"type":"codeinline","content":[{"type":"text","text":"Empire"}]},{"type":"text","text":"兩個航空公司的飛機師,使用"},{"type":"codeinline","content":[{"type":"text","text":".filter()"}]},{"type":"text","text":"就是輕而易舉的事情!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"var rebels = pilots.filter(function (pilot) {\n return pilot.faction === \"Rebels\";\n});\nvar empire = pilots.filter(function (pilot) {\n return pilot.faction === \"Empire\";\n});"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"就這麼簡單,如果使用箭頭方法(ES6)就更加優雅了:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const rebels = pilots.filter(pilot => pilot.faction === \"Rebels\");\nconst empire = pilots.filter(pilot => pilot.faction === \"Empire\");"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其實原理很簡單,只要你的回調方法返回的是"},{"type":"codeinline","content":[{"type":"text","text":"true"}]},{"type":"text","text":",這個值或者對象就會在新的數組裏面了。如果返回的是"},{"type":"codeinline","content":[{"type":"text","text":"false"}]},{"type":"text","text":"就會被過濾掉了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"結合使用 .map(),.reduce(),.filter()"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"既然我們剛剛學到的三個函數都是可以用於數組的,並且"},{"type":"codeinline","content":[{"type":"text","text":".map()"}]},{"type":"text","text":"和"},{"type":"codeinline","content":[{"type":"text","text":".filter()"}]},{"type":"text","text":"都是返回數組的。那我們就可以串聯起來使用。不說多了上代碼試試!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們用一個有趣一點的數據試驗一下,假如現在我們有一個"},{"type":"codeinline","content":[{"type":"text","text":"星球大戰"}]},{"type":"text","text":"裏面的"},{"type":"codeinline","content":[{"type":"text","text":"人物"}]},{"type":"text","text":"的數組。每個字段的定義如下:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"- "},{"type":"codeinline","content":[{"type":"text","text":"Id"}]},{"type":"text","text":": 人物唯一ID"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"- "},{"type":"codeinline","content":[{"type":"text","text":"name"}]},{"type":"text","text":": 人物名字"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"- "},{"type":"codeinline","content":[{"type":"text","text":"pilotingScore"}]},{"type":"text","text":": 飛行能力指數"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"- "},{"type":"codeinline","content":[{"type":"text","text":"shootingScore"}]},{"type":"text","text":": 射擊能力指數"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"- "},{"type":"codeinline","content":[{"type":"text","text":"isForceUser"}]},{"type":"text","text":": 是否擁有隔空操控能力"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們的目標:獲取"},{"type":"codeinline","content":[{"type":"text","text":"擁有隔空操控能力的飛行員的總飛行能力指數"}]},{"type":"text","text":"。我們先分開一步一步實現這個目標!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先我們需要先獲取到擁有隔空操控能力的飛行員。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"var jediPersonnel = personnel.filter(function (person) {\n return person.isForceUser;\n});\n// 結果集: [{...}, {...}, {...}] (Luke, Ezra and Caleb)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這段代碼我們獲得了3個飛行員對象,分別都是擁有隔空操控能力的飛行員。使用這個對象我們來獲取每個飛行員的飛行能力指數值。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"var jediScores = jediPersonnel.map(function (jedi) {\n return jedi.pilotingScore + jedi.shootingScore;\n});\n// 結果: [154, 110, 156]"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"獲取到每個飛行員的飛行能力指數值後,我們就可以用累加器("},{"type":"codeinline","content":[{"type":"text","text":".reduce()"}]},{"type":"text","text":")獲取總飛行能力指數了。"}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"var totalJediScore = jediScores.reduce(function (acc, score) {\n return acc + score;\n}, 0);\n// 結果: 420"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏分開實現方式可以達到我們的目標,但是其實我們可以串聯起來,可以寫的更加簡潔又優雅!我們來玩玩更好玩的吧!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"var totalJediScore = personnel\n .filter(function (person) {\n return person.isForceUser;\n })\n .map(function (jedi) {\n return jedi.pilotingScore + jedi.shootingScore;\n })\n .reduce(function (acc, score) {\n return acc + score;\n }, 0);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這樣寫是不是很優雅!都被這段代碼給美到了!❤️"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果我們使用箭頭寫法ES6,就更加優雅了!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const totalJediScore = personnel\n .filter(person => person.isForceUser)\n .map(jedi => jedi.pilotingScore + jedi.shootingScore)\n .reduce((acc, score) => acc + score, 0);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"哇!代碼原來可以寫的那麼優雅的麼?!想不到吧?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其實我們只需要使用"},{"type":"codeinline","content":[{"type":"text","text":".reduce()"}]},{"type":"text","text":"就可以得到我們的目標結果了,以上例子做爲教學例子,所以使用了3個我們學到的函數。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":">"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們來看看只用"},{"type":"codeinline","content":[{"type":"text","text":".reduce()"}]},{"type":"text","text":"怎麼實現的,來我們一起來刷新一下三觀吧!"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"const totalJediScore = personnel.reduce((acc, person) => person.isForceUser ? acc + person.pilotingScore + person.shootingScore : acc, 0);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"不敢想象吧?一行就搞定一個功能不是夢!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"爲什麼拋棄 .forEach()?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其實我一開始寫前端的時候也是一頓擼,來個數組都是擼個for循環,解決一切數組處理問題。但是近幾年我開始步入前後端開發,API接口對接。發現數據處理越來越多,如果還是像以前那樣什麼都用for循環來處理數據,那其實數據處理的代碼就會越來越臃腫越來越複雜凌亂。所以我開始拋棄了"},{"type":"codeinline","content":[{"type":"text","text":".forEach()"}]},{"type":"text","text":"。開始做一個優雅的程序員!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲什麼使用"},{"type":"codeinline","content":[{"type":"text","text":".map()"}]},{"type":"text","text":","},{"type":"codeinline","content":[{"type":"text","text":".filter()"}]},{"type":"text","text":","},{"type":"codeinline","content":[{"type":"text","text":".reduce()"}]},{"type":"text","text":"寫代碼更優雅,更美觀呢?我們用一個實戰例子來對比一下吧。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假設現在我們對接一個接口,返回的數組裏面有兩個字段"},{"type":"codeinline","content":[{"type":"text","text":"name:人的名稱"}]},{"type":"text","text":"和"},{"type":"codeinline","content":[{"type":"text","text":"title:對應的職位"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"var data = [\n {\n name: \"Jan Dodonna\",\n title: \"General\",\n },\n {\n name: \"Gial Ackbar\",\n title: \"Admiral\",\n },\n]"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"產品經理給到你的需求是隻需要展示這些人的職位稱呼。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當然這個時候有一些前端就會說“我只是個小小的前端,後端給我處理吧”。但是,這個接口其實是一個通用的接口,就是獲取這些員工的資料的,是在多個地方使用的。如果每一個頁面因爲需要展示的不一樣而要寫多一個接口給你,你覺得這樣好嗎?做爲一個優秀的前端工程師🦁️,這種小case你自己就可以很優雅的處理好了。而且,在一個優秀的團隊,後端確實是要考慮接口通用性的,這種爲了你的方便而給他們帶來更臃腫的接口是不可接受的。所以前端這個時候就是要重組數據了。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"假設現在產品給你的需求是員工列表中,要支持只展示員工職稱和員工信息的兩種顯示項。這個時候我們就要編寫一個數據組裝方法來跟進展示要求來改變數據格式。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"因爲這個“騷“需求,我們使用"},{"type":"codeinline","content":[{"type":"text","text":".forEach()"}]},{"type":"text","text":"來重組數據就相對比較麻煩了,而且代碼也會變得臃腫。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們忽略了組裝數據的方法,直接就當作我們已經寫好了一個組裝數據的方法爲"},{"type":"codeinline","content":[{"type":"text","text":"formatElement"}]},{"type":"text","text":"。如果我們用"},{"type":"codeinline","content":[{"type":"text","text":"forEach"}]},{"type":"text","text":",那首先就需要定義一個空數組來接收結果。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"var results = [];\ndata.forEach(function (element) {\n var formatted = formatElement(element);\n results.push(formatted);\n});"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所以我們需要兩個方法才能實現這個數據結果,但是爲什麼要寫的那麼臃腫呢?因爲"},{"type":"codeinline","content":[{"type":"text","text":"forEach"}]},{"type":"text","text":"並沒有返回值,單單就給你跑個循環,還需要自己"},{"type":"codeinline","content":[{"type":"text","text":"push"}]},{"type":"text","text":"值到預定義的變量裏面。其實一個方法就可以完成了,而且重點是一行代碼就完事了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"來使用我們新學的技巧,用"},{"type":"codeinline","content":[{"type":"text","text":".map()"}]},{"type":"text","text":"來實現就非常簡單優雅了。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"javascript"},"content":[{"type":"text","text":"var results = data.map(formatElement);"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"horizontalrule"},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"總結"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你學會了嗎?學會了就去嘗試用"},{"type":"codeinline","content":[{"type":"text","text":".map()"}]},{"type":"text","text":","},{"type":"codeinline","content":[{"type":"text","text":".reduce()"}]},{"type":"text","text":","},{"type":"codeinline","content":[{"type":"text","text":".filter()"}]},{"type":"text","text":"來替換你傳統的"},{"type":"codeinline","content":[{"type":"text","text":"for"}]},{"type":"text","text":"循環吧!我保證你的代碼會越來越簡潔,可讀性更高。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你喜歡我的這遍文章,記得繼續關注我的博客,下一遍文章我們開學習怎麼在JavaScript中使用"},{"type":"codeinline","content":[{"type":"text","text":".some()"}]},{"type":"text","text":"和"},{"type":"codeinline","content":[{"type":"text","text":".find()"}]},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"堅持做一個優雅的程序員,堅持每天敲代碼!"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c0/c069ed250daafee4ca8c738d4a71575b.png","alt":"和你一起終身學習","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章