SOURCE

/*
 Object.groupBy() allows us to easily group array items, 
 please try to implement it by yourself

 const items = [
  {
    id: 1,
    kind: 'a',
  },
  {
    id: 2,
    kind: 'b',
  },
  {
    id: 3,
    kind: 'a',
  }
];

const groups = Object.groupBy(items, ({kind}) => kind)
// {
//   a: [
//     {
//       id: 1,
//       kind: 'a'
//     },
//     {
//       id: 3,
//       kind: 'a'
//     }
//   ],
//   b: [
//     {
//       id: 2,
//       kind: 'b'
//     }
//   ]
// }
 */

// if (!Object.groupBy) {
//     Object.groupBy = function (items, callback) {
//         if (!items) {
//             throw new TypeError('Cannot read properties of null of undefined');
//         };

//         const result = {};
//         let index = 0;

//         for (let item of items) {
//             const key = callback(item, index++);

//             if (!result[key]) {
//                 result[key] = [];
//             }
//             result[key].push(item);
//         };

//         return result;
//     };
// };

if (!Object.groupBy) {
    Object.groupBy = function (items, callback) {
        if (!items) {
            throw new TypeError('First params must be Array');
        };

        const result = {};
        let index = 0;

        for (let item of items) {
            const key = callback(item, index++);

            if (!result[key]) {
                result[key] = [];
            }
            result[key].push(item);

        };

        return result;
    };
}

const items = [
    {
        id: 1,
        kind: 'a',
    },
    {
        id: 2,
        kind: 'b',
    },
    {
        id: 3,
        kind: 'a',
    }
];

const groups = Object.groupBy(items, ({ kind }) => kind);
console.log('groups', groups);
// {
//   a: [
//     {
//       id: 1,
//       kind: 'a'
//     },
//     {
//       id: 3,
//       kind: 'a'
//     }
//   ],
//   b: [
//     {
//       id: 2,
//       kind: 'b'
//     }
//   ]
// }
console 命令行工具 X clear

                    
>
console