Last active
March 2, 2021 00:32
-
-
Save seisvelas/6c832b4555504fc3413293d5dbf555e9 to your computer and use it in GitHub Desktop.
Group by chipset object problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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