Skip to content

Instantly share code, notes, and snippets.

View ghoussard's full-sized avatar

Grégoire Houssard ghoussard

  • France
  • 18:35 (UTC +01:00)
View GitHub Profile
@ghoussard
ghoussard / flat.js
Last active November 10, 2019 11:23
MongoDB JSON Document Recursive Flat
/**
* MongoDB JSON Document Recursive Flat
* @param {Object|Array} document MongoDB JSON Document
* @param {string|undefined} prefixKey used by recursivity to prefix deeper keys with parent keys
*/
const flat = (document, prefixKey = undefined) => {
const keys = Object.keys(document)
keys.forEach(key => {
const newKey = prefixKey ? `${prefixKey}_${key}` : key