Skip to content

Instantly share code, notes, and snippets.

@fpg1503
Created July 28, 2018 10:30
Show Gist options
  • Save fpg1503/f77348406e276ba37aedc9941b599eef to your computer and use it in GitHub Desktop.
Save fpg1503/f77348406e276ba37aedc9941b599eef to your computer and use it in GitHub Desktop.
Making an arbitrary JS Object nicer to work with
// Takes as input something like https://scontent-mad1-1.xx.fbcdn.net/v/t1.0-9/37899360_1857828550996984_1194250836729921536_o.jpg?_nc_cat=0&oh=04a7e8328932298e0f6eeec0764cd5b0&oe=5BD2D7EB
function flatBoletim(boletim) {
const innerAssign = (target, ...sources) => {
sources.forEach(source => {
Object.keys(source).forEach(key => target[key] = Object.assign(target[key] || {}, source[key]))
})
return target
}
const semesters = boletim.reduce((obj, current) => Object.assign({}, obj, {...current}), {})
const entries = Object.entries(semesters)
const splitEntries = entries.map(([key, value]) => [key.split('/'), value])
const grouped = splitEntries.reduce((obj, [[year, month], value]) => innerAssign({}, obj, {[year]: {[month]: value}}), {})
return grouped
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment