Created
November 20, 2017 17:35
-
-
Save lxm7/264ef43d5dae276b0099d0316a4c15bf to your computer and use it in GitHub Desktop.
Helpful map, reduce, filtering data
This file contains hidden or 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 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