Created
May 23, 2020 12:24
-
-
Save jellyninjadev/af1eb7bd53246dfd10b599ef57fd1ca9 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
// can run in https://www.typescriptlang.org/play | |
type Payload = { | |
results?: { | |
aggregation?: { | |
pie_aggregation?: { | |
items?: { | |
buckets: [{ | |
key: string | |
filtered: { | |
amount_stats: {sum: number} | |
}}] | |
} | |
} | |
} | |
} | |
} | |
const experiences = [{id: '1', name: 'name'}] | |
const payload: Payload = {} | |
const payload2: Payload = { | |
results: { | |
aggregation: { | |
pie_aggregation: { | |
items: { | |
buckets: [{key: '1', filtered: {amount_stats: {sum: 3}}}] | |
} | |
} | |
} | |
} | |
} | |
const toPie = (payload: Payload) => payload.results?.aggregation?.pie_aggregation?.items?.buckets.map(bucket => { | |
const experience = experiences.find(experience => experience.id === bucket.key) | |
if (!experience) return null | |
return { | |
name: experience.name, | |
y: bucket?.filtered?.amount_stats.sum ?? 0 | |
} | |
}) | |
const result = toPie(payload) | |
const result2 = toPie(payload2) | |
console.log('Undefined', result) | |
console.log('Defined', result2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment