On each iteration destructure the object, and get the Town
, and the rest of properties. Get the entries of the other properties, and iterate it with Array.forEach()
. As you did for Town
create the object for the property key and value as needed, and increment the count.
const obj=[{"Town":"Newton","PropertyType":"Multi Family","InvestmentType":"Homeownership","Perks":"Retail Store"}, {"Town":"South Surrey","PropertyType":"Multi Family","InvestmentType":"Investment Property","Perks":"Retail Store"}, {"Town":"South Surrey","PropertyType":"Multi Family","InvestmentType":"Investment Property","Perks":"Bus Station"}, {"Town":"South Surrey","PropertyType":"Condo","InvestmentType":"Homeownership","Perks":"Retail Store"}];
const result = Object.values(obj.reduce((a, { Town, ...rest }) => {
a[Town] = a[Town] || { Town };
Object.entries(rest)
.forEach(([k, v]) => {
a[Town][k] = a[Town][k] || {};
a[Town][k][v] = (a[Town][k][v] || 0) + 1;
});
return a;
}, {}));
console.log(result);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…