Skip to content

Instantly share code, notes, and snippets.

@danhyun
Created August 19, 2019 23:00
Show Gist options
  • Select an option

  • Save danhyun/7323845b6e8830d9af48d98a7102bcb7 to your computer and use it in GitHub Desktop.

Select an option

Save danhyun/7323845b6e8830d9af48d98a7102bcb7 to your computer and use it in GitHub Desktop.

Revisions

  1. danhyun created this gist Aug 19, 2019.
    19 changes: 19 additions & 0 deletions unpack.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    let unpack = (a) => Object.entries(a)
    .filter(([key, value]) => value.buckets)
    .flatMap(([key, value]) => value.buckets
    .flatMap(b => {
    let current = {
    [key] : b.key,
    doc_count: b.doc_count
    }
    let unpacked = unpack(b)
    if (unpacked.length == 0) {
    return [current]
    } else {
    return unpacked.flat().map((o) => Object.assign({}, current, o))
    }

    }))
    .filter(o => o.doc_count > 0)

    let flat = unpack(aggs)