Fastest way to flatten / un-flatten nested JSON objects

前端 未结 13 1290
孤城傲影
孤城傲影 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:20

    I added +/- 10-15% efficiency to the selected answer by minor code refactoring and moving the recursive function outside of the function namespace.

    See my question: Are namespaced functions reevaluated on every call? for why this slows nested functions down.

    function _flatten (target, obj, path) {
      var i, empty;
      if (obj.constructor === Object) {
        empty = true;
        for (i in obj) {
          empty = false;
          _flatten(target, obj[i], path ? path + '.' + i : i);
        }
        if (empty && path) {
          target[path] = {};
        }
      } 
      else if (obj.constructor === Array) {
        i = obj.length;
        if (i > 0) {
          while (i--) {
            _flatten(target, obj[i], path + '[' + i + ']');
          }
        } else {
          target[path] = [];
        }
      }
      else {
        target[path] = obj;
      }
    }
    
    function flatten (data) {
      var result = {};
      _flatten(result, data, null);
      return result;
    }
    

    See benchmark.

提交回复
热议问题