更优雅的编写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}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章