Skip to content

Instantly share code, notes, and snippets.

@seisvelas
Last active March 2, 2021 00:32
Show Gist options
  • Save seisvelas/6c832b4555504fc3413293d5dbf555e9 to your computer and use it in GitHub Desktop.
Save seisvelas/6c832b4555504fc3413293d5dbf555e9 to your computer and use it in GitHub Desktop.
Group by chipset object problem
//Given an array of VIZIO TV models, create a "groupByChipset" function
//that aggregates the model names by their "chipset" field:
//Example input:
const tvs = [
{
"modelName": "VIZIO D24f-F1",
"chipset": "5581"
},
{
"modelName": "VIZIO V405-H9",
"chipset": "5691"
},
{
"modelName": "VIZIO V405-H8",
"chipset": "5691"
}
];
//Example output:
/*
const tvs = {
"5581": ["VIZIO D24f-F1"],
"5691": ["VIZIO V405-H9, VIZIO V405-H8"]
};
*/
const groupByChipset = (tvs) =>
tvs.reduce( (acc, tv) => {
const model = tv['modelName']
const chipset = tv['chipset']
chipsetModels = acc[chipset] ?? [model]
if (!chipsetModels.includes(model)) {
chipsetModels.push(model)
}
acc[chipset] = chipsetModels
return acc
}, {})
console.log(groupByChipset(tvs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment