Created
July 28, 2018 10:30
-
-
Save fpg1503/f77348406e276ba37aedc9941b599eef to your computer and use it in GitHub Desktop.
Making an arbitrary JS Object nicer to work with
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
// 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