score:0

Accepted answer

You can use Array#reduce with a nested object to store the values for each combination of values for the two keys.

let arr=[{Suburb:"ardno",YearQtr:"2001Q2",Type:"House",NumSales:10},{Suburb:"ardno",YearQtr:"2001Q2",Type:"Unit",NumSales:20},{Suburb:"ardno",YearQtr:"2001Q2",Type:"Land",NumSales:30},{Suburb:"ardno",YearQtr:"2002Q1",Type:"Unit",NumSales:40},{Suburb:"ardno",YearQtr:"2002Q1",Type:"House",NumSales:50},{Suburb:"keilor",YearQtr:"2006Q4",Type:"House",NumSales:48},{Suburb:"keilor",YearQtr:"2007Q4",Type:"House",NumSales:61},{Suburb:"box",YearQtr:"2011Q2",Type:"House",NumSales:24},{Suburb:"box",YearQtr:"2011Q4",Type:"Unit",NumSales:26},{Suburb:"box",YearQtr:"2008Q3",Type:"Land",NumSales:5}];

let res = Object.values(arr.reduce((acc,{Suburb,YearQtr,Type,NumSales})=>{
  Object.assign(((acc[Suburb] = acc[Suburb] || {})[YearQtr] 
    = acc[Suburb][YearQtr] || {Suburb, YearQtr}), {[Type]: NumSales});
  return acc;
}, {})).flatMap(Object.values);
console.log(res);


Related Query

More Query from same tag