JavaScript通过两个属性将JSON对象分组并计数

假设我们有一个像这样的对象数组-

const arr = [
   {"location":"Kirrawee","identity_long":"student"},
   {"location":"Kirrawee","identity_long":"visitor"},
   {"location":"Kirrawee","identity_long":"visitor"},
   {"location":"Kirrawee","identity_long":"worker"},
   {"location":"Sutherland","identity_long":"student"},
   {"location":"Sutherland","identity_long":"resident"},
   {"location":"Sutherland","identity_long":"worker"},
   {"location":"Sutherland","identity_long":"resident"},
   {"location":"Miranda","identity_long":"resident"},
   {"location":"Miranda","identity_long":"worker"},
   {"location":"Miranda","identity_long":"student"},
   {"location":"Miranda","identity_long":""},
   {"location":"Miranda","identity_long":"worker"},
   {"location":"Miranda","identity_long":"resident"}
];

我们需要编写一个JavaScript函数,该函数接受一个这样的对象数组。函数应准备一个新的对象数组,其中所有(相同的)对象都基于location属性分组在一起。

并且应该为对象分配一个count属性,该属性包含它在对象原始数组中出现的次数。

因此,对于上述数组,输出应类似于-

const output = [
   {"location":"Kirrawee","identity":"student","count":1},
   {"location":"Kirrawee","identity":"visitor","count":2},
   {"location":"Kirrawee","identity":"worker","count":1},
   {"location":"Sutherland","identity":"student","count":1},
   {"location":"Sutherland","identity":"resident","count":2},
   {"location":"Sutherland","identity":"worker","count":1},
   {"location":"Miranda","identity":"resident","count":2},
   {"location":"Miranda","identity":"worker","count":2},
   {"location":"Miranda","identity":"student","count":1}
];

示例

为此的代码将是-

const arr = [
   {"location":"Kirrawee","identity_long":"student"},
   {"location":"Kirrawee","identity_long":"visitor"},
   {"location":"Kirrawee","identity_long":"visitor"},
   {"location":"Kirrawee","identity_long":"worker"},
   {"location":"Sutherland","identity_long":"student"},
   {"location":"Sutherland","identity_long":"resident"},
   {"location":"Sutherland","identity_long":"worker"},
   {"location":"Sutherland","identity_long":"resident"},
   {"location":"Miranda","identity_long":"resident"},
   {"location":"Miranda","identity_long":"worker"},
   {"location":"Miranda","identity_long":"student"},
   {"location":"Miranda","identity_long":""},
   {"location":"Miranda","identity_long":"worker"},
   {"location":"Miranda","identity_long":"resident"}
];
const groupArray = (arr = []) => {
   //创建映射
   let map = new Map()   for (let i = 0; i < arr.length; i++) {
      const s = JSON.stringify(arr[i]);
      if (!map.has(s)) {
         map.set(s, {
            location: arr[i].location,
            identity: arr[i].identity_long,
            count: 1,
         });
      } else {
         map.get(s).count++;
      }
   }
   const res = Array.from(map.values())
   return res;
};
console.log(groupArray(arr));

输出结果

控制台中的输出将是-

[
   { location: 'Kirrawee', identity: 'student', count: 1 },
   { location: 'Kirrawee', identity: 'visitor', count: 2 },
   { location: 'Kirrawee', identity: 'worker', count: 1 },
   { location: 'Sutherland', identity: 'student', count: 1 },
   { location: 'Sutherland', identity: 'resident', count: 2 },
   { location: 'Sutherland', identity: 'worker', count: 1 },
   { location: 'Miranda', identity: 'resident', count: 2 },
   { location: 'Miranda', identity: 'worker', count: 2 },
   { location: 'Miranda', identity: 'student', count: 1 },
   { location: 'Miranda', identity: '', count: 1 }
]