Fastest way to flatten / un-flatten nested JSON objects

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

    This code recursively flattens out JSON objects.

    I included my timing mechanism in the code and it gives me 1ms but I'm not sure if that's the most accurate one.

                var new_json = [{
                  "name": "fatima",
                  "age": 25,
                  "neighbour": {
                    "name": "taqi",
                    "location": "end of the street",
                    "property": {
                      "built in": 1990,
                      "owned": false,
                      "years on market": [1990, 1998, 2002, 2013],
                      "year short listed": [], //means never
                    }
                  },
                  "town": "Mountain View",
                  "state": "CA"
                },
                {
                  "name": "qianru",
                  "age": 20,
                  "neighbour": {
                    "name": "joe",
                    "location": "opposite to the park",
                    "property": {
                      "built in": 2011,
                      "owned": true,
                      "years on market": [1996, 2011],
                      "year short listed": [], //means never
                    }
                  },
                  "town": "Pittsburgh",
                  "state": "PA"
                }]
    
                function flatten(json, flattened, str_key) {
                    for (var key in json) {
                      if (json.hasOwnProperty(key)) {
                        if (json[key] instanceof Object && json[key] != "") {
                          flatten(json[key], flattened, str_key + "." + key);
                        } else {
                          flattened[str_key + "." + key] = json[key];
                        }
                      }
                    }
                }
    
            var flattened = {};
            console.time('flatten'); 
            flatten(new_json, flattened, "");
            console.timeEnd('flatten');
    
            for (var key in flattened){
              console.log(key + ": " + flattened[key]);
            }
    

    Output:

    flatten: 1ms
    .0.name: fatima
    .0.age: 25
    .0.neighbour.name: taqi
    .0.neighbour.location: end of the street
    .0.neighbour.property.built in: 1990
    .0.neighbour.property.owned: false
    .0.neighbour.property.years on market.0: 1990
    .0.neighbour.property.years on market.1: 1998
    .0.neighbour.property.years on market.2: 2002
    .0.neighbour.property.years on market.3: 2013
    .0.neighbour.property.year short listed: 
    .0.town: Mountain View
    .0.state: CA
    .1.name: qianru
    .1.age: 20
    .1.neighbour.name: joe
    .1.neighbour.location: opposite to the park
    .1.neighbour.property.built in: 2011
    .1.neighbour.property.owned: true
    .1.neighbour.property.years on market.0: 1996
    .1.neighbour.property.years on market.1: 2011
    .1.neighbour.property.year short listed: 
    .1.town: Pittsburgh
    .1.state: PA
    

提交回复
热议问题