Last active
May 8, 2020 08:01
-
-
Save giacomoalonzi/83d64ae44c7eb9ff84781bdf5f2be45e to your computer and use it in GitHub Desktop.
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
const input = [ | |
{ type: 'a', distance: 5 }, | |
{ type: 'c', distance: 3 }, | |
{ type: 'b', distance: 6 }, | |
{ type: 'c', distance: 12 }, | |
{ type: 'b', distance: 1 }, | |
{ type: 'a', distance: 5 }, | |
{ type: 'b', distance: 6 }, | |
{ type: 'a', distance: -1 }, | |
{ type: 'c', distance: 9 }, | |
{ type: 'd', distance: 10 } | |
] | |
function distances(input) { | |
return input.filter(i => i.type !== 'a').reduce(({near, medium, far}, currVal) => { | |
return ({ | |
near: currVal.distance >= 0 && currVal.distance < 4 ? [...near, currVal] : near, | |
medium: currVal.distance >= 4 && currVal.distance < 8 ? [...medium, currVal] : medium, | |
far: currVal.distance >= 8 && currVal.distance <= 10 ? [...far, currVal] : far | |
}) | |
}, { near: [], medium: [], far: [] }) | |
} | |
distances(input) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment