Skip to content

Instantly share code, notes, and snippets.

@giacomoalonzi
Last active May 8, 2020 08:01
Show Gist options
  • Save giacomoalonzi/83d64ae44c7eb9ff84781bdf5f2be45e to your computer and use it in GitHub Desktop.
Save giacomoalonzi/83d64ae44c7eb9ff84781bdf5f2be45e to your computer and use it in GitHub Desktop.
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