vardha
vardha

Reputation: 414

parse and construct a complicated json

i have an array like below,

array=[ 
  { 'mm': '1', exp: 'exp1' },
  { 'mm': '2', exp: 'exp2' },
  { 'mm': [ '1', '3', '7' ], exp: 'exp3' },
  { 'mm': [ '1', '2', '4', '6' ], exp: 'exp4' },
  { 'mm': [ '1', '3', '2' ], exp: 'exp5' },
  { 'mm': [ '8', '2', '9' ], exp: 'exp6' },
  { 'mm': [ '4', '7', '1', '2' ], exp: 'exp7' },
  { 'mm': [ '5', '6', '2', '4', '3', '8', '1' ], exp: 'exp8' } 
]

i need to restructure this array (turn keys to values and values to keys) like below(cannot hardcode any values in 'mm' or 'exp' keys as they always keep changing,

    [ 
      { 'mm': '1', exp: ['exp1','exp3','exp4',','exp5','exp7','exp8'] },
      { 'mm': '2', exp: ['exp2','exp4','exp5','exp6','exp7','exp8'] },
      { 'mm': '3', exp: ['exp3','exp5','exp8'] },
      { 'mm':'4', exp:['exp4','exp7','exp8'] },
      { 'mm':'5', exp:['exp8'] },
      { 'mm':'6', exp:['exp4','exp8'] },
      { 'mm':'7', exp: ['exp3','exp7'] },
      { 'mm':'8', exp: ['exp6','exp8] },
      { 'mm':'9', exp: ['exp6'] }
] 

you get the idea, hopefully, lemme know if you find an efficient solution while i try to get this done myself.

Upvotes: 0

Views: 45

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386520

This proposal uses two nested loops and an object for the reference to the right element of the result array.

var array = [{ 'mm': '1', exp: 'exp1' }, { 'mm': '2', exp: 'exp2' }, { 'mm': ['1', '3', '7'], exp: 'exp3' }, { 'mm': ['1', '2', '4', '6'], exp: 'exp4' }, { 'mm': ['1', '3', '2'], exp: 'exp5' }, { 'mm': ['8', '2', '9'], exp: 'exp6' }, { 'mm': ['4', '7', '1', '2'], exp: 'exp7' }, { 'mm': ['5', '6', '2', '4', '3', '8', '1'], exp: 'exp8' }],
    pivot = function (array) {
        var r = [], o = {};
        array.forEach(function (a) {
            [].concat(a.mm).forEach(function (b) {
                if (!o[b]) {
                    o[b] = { mm: b, exp: [] };
                    r.push(o[b]);
                }
                o[b].exp.push(a.exp);
            });
        });
        return r;
    }(array);

document.write('<pre>' + JSON.stringify(pivot, 0, 4) + '</pre>');

Upvotes: 2

user3297291
user3297291

Reputation: 23372

I don't really understand what you mean by 'cannot hardcode any values', but I thought I'd share an example solution anyway. I've not really optimized it for performance: it's mainly to show a general direction.

var data =[ 
  { 'mm': '1', exp: 'exp1' },
  { 'mm': '2', exp: 'exp2' },
  { 'mm': [ '1', '3', '7' ], exp: 'exp3' },
  { 'mm': [ '1', '2', '4', '6' ], exp: 'exp4' },
  { 'mm': [ '1', '3', '2' ], exp: 'exp5' },
  { 'mm': [ '8', '2', '9' ], exp: 'exp6' },
  { 'mm': [ '4', '7', '1', '2' ], exp: 'exp7' },
  { 'mm': [ '5', '6', '2', '4', '3', '8', '1' ], exp: 'exp8' } 
];

var tempData = {};
var newData = [];

data.forEach(function(dataPoint) {
    var arrPoint = {
        mm: [].concat(dataPoint.mm),
        exp: [].concat(dataPoint.exp)
    };

    arrPoint.mm.forEach(function (code) {
        if (!tempData[code]) {
            tempData[code] = {
                mm: code,
                exp: []
            }
        }

        var vals = tempData[code].exp;

        arrPoint.exp.forEach(function(val) {
            if (vals.indexOf(val) === -1) {
                vals.push(val);
            }
        });

    });
});


for (var propName in tempData) {
    if (tempData.hasOwnProperty(propName)) {
        newData.push(tempData[propName]);
    }
}

document.write(JSON.stringify(newData));

Next time, it's probably best if you try for yourself first, before asking. That way, your question can be a bit less broad..

Upvotes: 1

Related Questions