level 12
// 17年看过类似问题 思路来源一位大佬写的答案
let aJson = [
{type: "a", name: "小明"},
{type: "a", name: "小军"},
{type: "a", name: "小七"},
{type: "b", name: "小说"},
{type: "b", name: "小车"},
{type: "c", name: "小张"}
];
let aNewJson = Object.values(aJson.reduce((p, n) => (p[n.type] ? p[n.type].arr.push(n) : (p[n.type] = {leixing: n.type, arr: [n]}), p), {}));
console.log(aNewJson);
console.log(JSON.stringify(aNewJson));
2020年04月20日 11点04分
4