Flattening JSON with Javascript


1 comment

  • Steve Klett Strategic Partner
    Celigo University Level 4: Legendary
    Answer Pro
    Great Answer
    Top Contributor
    Awesome Follow-up
    function postResponseMap (options) {
    const res = options.postResponseMapData.forEach(l1 => {
    l1.data.forEach(l2 => {
    l2.data.reduce((acc, cur) => {
    acc[cur.name] = cur.values
    return acc
    }, {})

    I didn't test that, but that's how I'd do it. Avoid index access like that, even if you know there will only be a single result, it's cleaner (IMO) to make it dynamic as it will also handle empty arrays where index access will fail.


Please sign in to leave a comment.