reduce使用まとめ

4137 ワード

  • アキュムレータ
  • const sum = [1,2,3,4].reduce((accu, cur) => accu + cur, 10);  // 20
    
  • 配列デ重量
  • //     
    const uniqueArr = [1,2,3,4,2,3,2,3,5,1].reduce((accu, cur) => !accu.includes(cur) ? accu.concat(cur) : accu, []);
    // [1,2,3,4,5]
    
    //    :
    [...new Set([1,2,3,4,2,3,2,3,5,1])];
    Array.from(new Set(1,2,3,4,2,3,2,3,5,1));
    
    
    //     (    object  )
    const arr = [
      {team: 'The clippers', player:'Paul' },
      {team: 'The cavaliers', player:'James' },
      {team: 'The nets', player:'Durant' },
      {team: 'The lakers', player:'James' }
    ];
    const uniqueArr = arr.reduce((accu, cur) => {
      if (!accu.find((item) => item.player === cur.player)) {
        accu.concat(cur);
      }
      return accu;
    }, []);  
    // [
    // {team: 'The clippers', player:'Paul'},
    // {team: 'The cavaliers', player:'James'},
    // {team: 'The nets', player:'Durant'}
    // ]
  • 再帰的に、配列降下
  • を実現する.
    const flattenArray = (arr) => {
      arr.reduce((accu, cur) => Array.isArray(curr) ? accu.concat(func(curr)) : accu.concat(curr), []);
    };
    flattenArray([8, [5, 4], 1, 3, [7, 5, 10, [3, 6, 2]], 4, 3, 2, 4]); // // [ 8, 5, 4, 1, 3, 7, 5, 10, 3, 6, 2, 4, 3, 2, 4 ]
  • 属性グループ
  • var people = [
      {team: 'The clippers', player:'Paul'},
      {team: 'The cavaliers', player:'James'},
      {team: 'The nets', player:'Durant'},
      {team: 'The lakers', player:'James'}
    ];
    
    function groupBy(objectArray, property) {
      return objectArray.reduce(function (acc, obj) {
        var key = obj[property];
        if (!acc[key]) {
          acc[key] = [];
        }
        acc[key].push(obj);
        return acc;
      }, {});
    }
    
    var groupedPlayer = groupBy(people, 'player');
    // groupedPlayer is:
    // {
    //   James: [
    //     {team: 'The lakers', player:'James'},
    //     {team: 'The cavaliers', player:'James'},
    //   ],
    //   Paul: [{team: 'The clippers', player:'Paul'}],
    //   Durant: [team: 'The nets', player:'Durant']
    // }
  • 周波数統計
  • const names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];
    const countedNames = names.reduce(function (allNames, name) {
      name in allNames ? allNames[name]++ : allNames[name] = 1;
      return allNames;
    }, {});
    // countedNames is:
    // { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }
    map、filter、forEachなどの機能を実現します.
  • reduce map
  • を実現しました.
    Array.prototype.reduceMap = function(callback) {
      return this.reduce((accu, cur, index) => accu.concat(callback(cur, index));
      }, []);
    };
    
    [1,2,3,4].reduceMap((item, index) => item + index); // [1,3,5,7]
  • reduce forEach
  • を実現しました.
    
    Array.prototype.reduceForEach = function(callback) {
      this.reduce((accu, cur, index, array) => {
        callback(cur, index, array);
      }, []);
    };
    
    [1, 2, 3, 4].reduceForEach((item, index, array) => {
      console.log(item, index, array)
    });
    // 1 0 [1,2,3,4]
    // 2 1 [1,2,3,4]
    // 3 2 [1,2,3,4]
    // 4 3 [1,2,3,4]
  • reduce filter
  • を実現します.
    Array.prototype.reduceFilter = function (callback) {
       return this.reduce((accu, cur, index, array) => callback(cur, index, array)) ? accu.concat(cur) : accu, []);
    };
    
    [1, 2, 3, 4].reduceFilter(item => item % 2 === 0); 
    // [2, 4]
  • reduce find
  • を実現します.
    const obj = [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }]
    Array.prototype.reduceFind = function (callback) {
      return this.reduce((accu, cur, index, array) => {
        if (callback(cur, index, array)) {
          if (accu instanceof Array && accu.length === 0) {
            accu = cur;
          }
        }    
        if ((index === array.length - 1) && accu instanceof Array && accu.length === 0) {
          accu = undefined;
        }
        return accu;
      }, []);
    };
    obj.reduceFind(item => item.a % 2 == 0); // {a: 2}
    obj.reduceFind(item => item.a % 9 == 0); // undefined