Skip to content

Instantly share code, notes, and snippets.

@allucardster
Created October 27, 2019 08:11
Show Gist options
  • Save allucardster/54b2c9db5d2632e7b5516e81ef67e285 to your computer and use it in GitHub Desktop.
Save allucardster/54b2c9db5d2632e7b5516e81ef67e285 to your computer and use it in GitHub Desktop.
Create a colors config object from a `object[]` and `selected[string]` values
const colors = [
{
"color": "black",
"total": 99
},
{
"color": "brown",
"total": 93
},
{
"color": "white",
"total": 66
}
];
const selected = ['brown'];
const config = Object.fromEntries(Array.from(
colors.map(result => result.color),
color => [color, selected.indexOf(color) >= 0]
));
console.log(config);
// expected: Object { black: false, brown: true, white: false }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment