Fastest way to flatten / un-flatten nested JSON objects

前端 未结 13 1302
孤城傲影
孤城傲影 2020-11-21 20:32

I threw some code together to flatten and un-flatten complex/nested JSON objects. It works, but it\'s a bit slow (triggers the \'long script\' warning).

For the flat

13条回答
  •  一生所求
    2020-11-21 21:27

    3 ½ Years later...

    For my own project I wanted to flatten JSON objects in mongoDB dot notation and came up with a simple solution:

    /**
     * Recursively flattens a JSON object using dot notation.
     *
     * NOTE: input must be an object as described by JSON spec. Arbitrary
     * JS objects (e.g. {a: () => 42}) may result in unexpected output.
     * MOREOVER, it removes keys with empty objects/arrays as value (see
     * examples bellow).
     *
     * @example
     * // returns {a:1, 'b.0.c': 2, 'b.0.d.e': 3, 'b.1': 4}
     * flatten({a: 1, b: [{c: 2, d: {e: 3}}, 4]})
     * // returns {a:1, 'b.0.c': 2, 'b.0.d.e.0': true, 'b.0.d.e.1': false, 'b.0.d.e.2.f': 1}
     * flatten({a: 1, b: [{c: 2, d: {e: [true, false, {f: 1}]}}]})
     * // return {a: 1}
     * flatten({a: 1, b: [], c: {}})
     *
     * @param obj item to be flattened
     * @param {Array.string} [prefix=[]] chain of prefix joined with a dot and prepended to key
     * @param {Object} [current={}] result of flatten during the recursion
     *
     * @see https://docs.mongodb.com/manual/core/document/#dot-notation
     */
    function flatten (obj, prefix, current) {
      prefix = prefix || []
      current = current || {}
    
      // Remember kids, null is also an object!
      if (typeof (obj) === 'object' && obj !== null) {
        Object.keys(obj).forEach(key => {
          this.flatten(obj[key], prefix.concat(key), current)
        })
      } else {
        current[prefix.join('.')] = obj
      }
    
      return current
    }
    

    Features and/or caveats

    • It only accepts JSON objects. So if you pass something like {a: () => {}} you might not get what you wanted!
    • It removes empty arrays and objects. So this {a: {}, b: []} is flattened to {}.

提交回复
热议问题