专栏名称: SegmentFault思否
SegmentFault (www.sf.gg)开发者社区,是中国年轻开发者喜爱的极客社区,我们为开发者提供最纯粹的技术交流和分享平台。
目录
相关文章推荐
程序员的那些事  ·  GPU:DeepSeek ... ·  7 小时前  
OSC开源社区  ·  Bun ... ·  昨天  
程序员的那些事  ·  OpenAI ... ·  2 天前  
程序猿  ·  “未来 3 年内,Python 在 AI ... ·  5 天前  
51好读  ›  专栏  ›  SegmentFault思否

ES6 换种思路处理数据

SegmentFault思否  · 公众号  · 程序员  · 2018-02-09 08:00

正文

Handle javascript data structures with map/reduce

看完本文,希望可以写出更加漂亮、简洁、函数式的代码🤞

reduce

reduce 可以用来 汇总 数据。

  1. const customer = [

  2.  {id: 1, count: 2},

  3.  {id: 2, count: 89},

  4.  {id: 3, count: 1}

  5. ];

  6. const totalCount = customer.reduce((total, item) =>

  7.  total + item.count,

  8.  0 // total 的初始值

  9. );

  10. // now totalCount = 92

把一个对象数组变成一个以数组中各个对象的 id 为属性名,对象本身为属性值的对象。haoduoshipin(http://haoduoshipin.com/videos/240/)

  1. let products = [

  2.  {

  3.    id: '123',

  4.    name: '苹果'

  5.  },

  6.  {

  7.    id: '345',

  8.    name: '橘子'

  9.  }

  10. ];

  11. const productsById = products.reduce(

  12.  (obj, product) => {

  13.    obj[product.id] = product

  14.    return obj

  15.  },

  16.  {}

  17. );

  18. console.log('result', productsById);

map

map 可以理解为是数组的转换器,依次对数组中的每个元素做变换进而得到一个新的数组。

  1. const integers = [1, 2, 3, 4, 6, 7];

  2. const twoXIntegers = integers.map(i => i*2);

  3. // twoXIntegers are now [2, 4, 6, 8, 12, 14]

  4. // integers数组并不会受到影响

find🔥

筛选出数组中的 个别 元素。

  1. const posts = [

  2.  {id: 1, title: 'Title 1'},

  3.  {id: 2, title: 'Title 2'},

  4. ];

  5. // find the title of post whose id is 1

  6. const title = posts.find(p => p.id === 1).title;

唉~ 使用了半年的 es6才发现有这么好用的东西,译者傻缺还像下面这么写过呢。

  1. const posts = [

  2.  {id: 1, title: 'Title 1'},

  3.  {id: 2, title: 'Title 2'},

  4. ];

  5. const title = posts.filter(item => item.id === 1)[0].title;

filter

筛选出数组中 某些 符合条件的元素组成新的数组。

  1. const integers = [1, 2, 3, 4, 6, 7];

  2. const evenIntegers = integers.filter(i => i/2 === 0);

  3. // evenIntegers are [2, 4, 6]

请大家自行思考下 filter find 的区别

数组concat

  1. const arr1 = [1, 2, 3, 4, 5];

  2. const arr2 = [6, 7, 8, 9, 0];

  3. const arrTarget = [...arr1, ...arr2];

  4. // [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

对象操作

  1. function operation(query, option = {}) {

  2.    const param = {...query, ...option};

  3.    // ....

  4.    return param;

  5. }

  6. let opt = {startTime: 123455555, endTime: 113345555};

  7. let q = {name: '一步', age: 'xxx'};

  8. operation(q, opt);

  9. // {name: "一步", age: "xxx", startTime: 123455555, endTime: 113345555}

对象是引用传参的,所以函数内部应该尽可能的保证传入的参数不受到污染。

为对象动态地添加字段

  1. const dynamicKey = 'wearsSpectacles';

  2. const user = {name: 'Shivek Khurana'};

  3. const updatedUser = {...user, [dynamicKey]: true};

  4. // updatedUser is {name: 'Shivek Khurana', wearsSpectacles: true}

将对象转换为query字符串👏

  1. const params = {color: 'red', minPrice: 8000, maxPrice: 10000};

  2. const query = '?' + Object.keys(params)

  3.  .map(k =>

  4.    encodeURIComponent(k) + '=' + encodeURIComponent(params[k])

  5.  )

  6.  .join('&')

  7. ;

  8. // encodeURIComponent encodes special characters like spaces, hashes

  9. // query is now "color=red&minPrice=8000&maxPrice=10000"

得到对象数组的元素 index

  1. const posts = [

  2.  {id: 13, title: 'Title 221'},

  3.  {id: 5, title: 'Title 102'},

  4.  {id: 131, title: 'Title 18'},

  5.  {id:







请到「今天看啥」查看全文