{"action":"opened","issue":{"url":"https://api.github.com/repos/imeay/blog/issues/4","repository_url":"https://api.github.com/repos/imeay/blog","labels_url":"https://api.github.com/repos/imeay/blog/issues/4/labels{/name}","comments_url":"https://api.github.com/repos/imeay/blog/issues/4/comments","events_url":"https://api.github.com/repos/imeay/blog/issues/4/events","html_url":"https://github.com/imeay/blog/issues/4","id":254883465,"number":4,"title":"利用lodash提升开发效率","user":{"login":"imeay","id":6468554,"avatar_url":"https://avatars1.githubusercontent.com/u/6468554?v=4","gravatar_id":"","url":"https://api.github.com/users/imeay","html_url":"https://github.com/imeay","followers_url":"https://api.github.com/users/imeay/followers","following_url":"https://api.github.com/users/imeay/following{/other_user}","gists_url":"https://api.github.com/users/imeay/gists{/gist_id}","starred_url":"https://api.github.com/users/imeay/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/imeay/subscriptions","organizations_url":"https://api.github.com/users/imeay/orgs","repos_url":"https://api.github.com/users/imeay/repos","events_url":"https://api.github.com/users/imeay/events{/privacy}","received_events_url":"https://api.github.com/users/imeay/received_events","type":"User","site_admin":false},"labels":[{"id":682288841,"url":"https://api.github.com/repos/imeay/blog/labels/lodash","name":"lodash","color":"1d76db","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-09-03T16:03:23Z","updated_at":"2017-09-03T16:03:23Z","closed_at":null,"author_association":"OWNER","body":"# 为什么要使用lodash\r\n开发过程中，总会遇到需要对数组、对象进行操作时候。虽然可以自己可以写一些方法来实现，但无疑会拖慢开发效率。lodash它提供了一些方法来方便我们对数组，对象的操作，可以让我们把重心放在业务逻辑上。\r\n# 怎样引入lodash\r\n* ES6 模块\r\n```\r\nimport * as _  from 'lodash';\r\n```\r\n* commonjs\r\n```\r\nconst _ = require('lodash');\r\n```\r\n# 平时用的比较多的\r\n## 产生新的数组\r\n* map 用于产生新的数组\r\n* filter 用于过滤，并产生过滤后的数组\r\n\r\n## 遍历数组并生成新的数据结构\r\n* _.reduce \r\n\r\n## 数组转对象\r\n* keyBy \r\n* groupBy\r\n## 查找某个对象在数组中的位置\r\n* findIndex\r\n  * demo\r\n     > 找出id为2的对象并删除\r\n ```\r\nlet companies = [\r\n  {id: 1, name: '3G门户'},\r\n  {id: 2, name: '足球控'},\r\n  {id: 3, name: '编程猫'},\r\n];\r\n \r\nconst index = _.findIndex(companies, function (item) { \r\n   return item.id == 2; \r\n});\r\n\r\nconst new_companies = [...companies.slice(0,index), ...companies.slice(index + 1)];\r\nconsole.log(new_companies);\r\n// [{ id: 1, name: '3G门户'}, {id: 3, name: '编程猫'}]\r\n```\r\n\r\n## 合并多个数组\r\n* union\r\n```\r\nconst arr_1 = [1, 2];\r\nconst arr_2 = [3, 4];\r\nconst arr_3 = _.union(arr_1, arr_2);\r\nconsole.log(arr_3);\r\n// [1, 2, 3, 4]\r\n```\r\n## 快速找出两个数组不同的元素\r\n* difference\r\n```\r\nconst arr_1 = [1, 2, 3];\r\nconst arr_2 = [3, 4];\r\nconst arr_3 = _.difference(arr_1, arr_2);\r\nconst arr_4 = _.difference(arr_2, arr_1);\r\nconsole.log(arr_3); // [1, 2]\r\nconsole.log(arr_4); // [4]\r\n```\r\n# 陆续补充中\r\n总体来说 lodash 还是蛮好的，提升工作效率必备工具"}}
{"actor":{"display_login":"imeay"}}