2

For the following array of Objects

[
 {"Corp": "ABC", "T1": "HW A/V", "T2": "A/V System","T3": "Audio","Prod": "Audio System","Mfr": "One"},
 {"Corp": "ABC", "T1": "HW A/V", "T2": "A/V System","T3": "Audio","Prod": "Audio System","Mfr": "Two"},
 {"Corp": "ABC", "T1": "HW A/V", "T2": "A/V System","T3": "Video","Prod": "Video System","Mfr": "Other"}
]

I intend to get a unflatten object like the one below :

[{
"Corp": "ABC",
"List": [{
    "T1": "HW A/V",
    "List": [{
        "T2": "A/V System",
        "List": [{
            "T3": "Audio",
            "List": [{
                "Prod": "Audio System",
                "List": [
                    {"Mfr": "One"},
                    {"Mfr": "Two"}
                ]
            }]
        },
        {
            "T3": "Video",
            "List": [{
                "Prod": "Video System",
                "List": [
                    {"Mfr": "Other"}
                ]
            }]
        }]
    }]
}]

}] I did get the result I intend to get as described above. I used underscore to get the result. Following code snippet did the job for me :

var items = _.map(_.groupBy(itemList, 'Corp'), function (a) {
        return _.extend(_.pick(a[0], 'Corp'), {
            List: _.map(_.groupBy(a, 'T1'), function (b) {
                return _.extend(_.pick(b[0], 'T1'), {
                    List: _.map(_.groupBy(b, 'T2'), function (c) {
                        return _.extend(_.pick(c[0], 'T2'), {
                            List: _.map(_.groupBy(c, 'T3'), function (d) {
                                return _.extend(_.pick(d[0], 'T3'), {
                                    List: _.map(_.groupBy(d, 'Prod'), function (e) {
                                        return _.extend(_.pick(e[0], 'Prod'), {
                                            List: _.map(e, function (elem) {
                                                return _.pick(elem, 'Mfr')
                                            })
                                        });
                                    })
                                });
                            })
                        });
                    })
                });
            })
        });
    });

Now, So All I am looking for, is if someone can enhance my solution. I want to optimize both space and time for this process.

PS: In morning, I had asked a similar question requesting for the solution, and that question was marked as TOO BROAD and was put on HOLD, so I have added my solution with this question, now all I am looking for is a better solution.

Thanks

4
  • There's no such thing as a "JSON Object" Commented Aug 8, 2016 at 7:42
  • 2
    Code Review might be a better place to ask this question since there isn't an actual problem, that you want to improve already working code. Commented Aug 8, 2016 at 7:45
  • @JLRishe oh... My bad ... thats because I copied some part of the question from my earlier question. Commented Aug 8, 2016 at 8:05
  • hi, i have a tried a bit on your problem, can you have a look at this,. jsfiddle.net/Nageshwar521/gee2e7cv Commented Aug 8, 2016 at 10:38

1 Answer 1

2

In order to avoid imbricated statements, you may want to define a list of 'expand keys' and iterate on them.

It's half-tempting to automatically extract the keys with something like:

expandKeys = _.keys(itemList[0]);

But since Javascript doesn't guarantee the order of the keys in an object, you really should define this list explicitly instead.

Below is some example code.

var itemList = [
  {"Corp": "ABC", "T1": "HW A/V", "T2": "A/V System", "T3": "Audio", "Prod": "Audio System", "Mfr": "One"},
  {"Corp": "ABC", "T1": "HW A/V", "T2": "A/V System", "T3": "Audio", "Prod": "Audio System", "Mfr": "Two"},
  {"Corp": "ABC", "T1": "HW A/V", "T2": "A/V System", "T3": "Video", "Prod": "Video System", "Mfr": "Other"}
];

var expandKeys = [ 'Corp', 'T1', 'T2', 'T3', 'Prod', 'Mfr' ];

function expandList(list, keys) {
  var node, obj, root = {};

  _.each(list, function(item) {
    obj = root;
    _.each(keys, function(key) {
      obj = (obj.List = obj.List || []);
      node = _.find(obj, function(i) { return i[key] == item[key]; });

      if(node === undefined) {
        obj.push(node = {});
        node[key] = item[key];
      }
      obj = node;
    });
  });
  return root.List;
}

var res = expandList(itemList, expandKeys);
console.log(res);
<script src="http://underscorejs.org/underscore-min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.