Skip to content

Instantly share code, notes, and snippets.

@lxm7
Created November 20, 2017 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lxm7/264ef43d5dae276b0099d0316a4c15bf to your computer and use it in GitHub Desktop.
Save lxm7/264ef43d5dae276b0099d0316a4c15bf to your computer and use it in GitHub Desktop.
Helpful map, reduce, filtering data
const prizesArr = [{id:1, value: "100"}, {id:2, value: "10"}, {id:3, value: "100"}];
let values = [] as string[];
// This checks for the highest occurence of the property value in each prize.
// They're wont be any higher than 3, and this 3x occurence is the prize
// amount that's passed to the promotionTriggerOptIn function
return prizesArr
.reduce((prev, curr) => { prev.push(curr.value); return prev; }, values = [])
.sort((a, b) => values.filter((v) => v === a).length - values.filter((v) => v === b).length)
.pop();
// '100'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment