JSFiddle - React, Tailwind, and code Playground

by ananyaneogi

HTML

<div>

</div>

JavaScript

const HarryPotter = {
  name: "Harry Potter",
  house: "Gryffindor",
  points: 40
};
const HermioneGranger = {
  name: "Hermione Granger",
  house: "Gryffindor",
  points: 140
};
const DracoMalfoy = {
  name: "Draco Malfoy",
  house: "Slytherin",
  points: -20
};
const TaylorSwift = {
  name: "Taylor Swift",
  house: "Slytherin",
  points: 100
};
const LinManuelMiranda = {
  name: "Lin Manuel Miranda",
  house: "Slytherin",
  points: 5000
};
const CedricDiggory = {
  name: "Cedric Diggory",
  house: "Hufflepuff",
  points: 12
};
const SallyPerks = {
  name: "Sally Perks",
  house: "Hufflepuff",
  points: 15
};
const LunaLovegood = {
  name: "Luna Lovegood",
  house: "Ravenclaw",
  points: 100
};
const ChoChang = {
  name: "Cho Chang",
  house: "Ravenclaw",
  points: 100
};
const wizards = [
  HarryPotter,
  HermioneGranger,
  DracoMalfoy,
  LinManuelMiranda,
  TaylorSwift,
  CedricDiggory,
  SallyPerks,
  LunaLovegood,
  ChoChang
];

function keyBy(list, key) {
  return list.reduce((acc, item) => {
    const index = item[key];
    acc[index] = item;
    return acc;
  }, {})
} 

const keyByRes = keyBy(wizards, 'name');


function groupBy(list, condition) {
	return list.reduce((acc, item) => {
  	const index = condition(item);
    if (!acc[index]) {
    	acc[index] = [];
    }
    acc[index].push(item);
    return acc;
  }, {});
}

console.log(groupBy(wizards, wizard => wizard.house))