Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement this logic

Tags:

javascript

I have object structure like below

var obj = {
   a : 1,
   b : [x,y,z],
   c : [0,1,3],
   d : ['%','-','+']
}

I want to convert that object to below format

{
  1 : {
     x : {
         0 : ['%','-','+'], // Last index remains as an array
         1 : ['%','-','+'],
         3 : ['%','-','+']
     },
     y : {
         0 : ['%','-','+'], // Last index remains as an array
         1 : ['%','-','+'],
         3 : ['%','-','+']
     },
     z : {
         0 : ['%','-','+'], // Last index remains as an array
         1 : ['%','-','+'],
         3 : ['%','-','+']
     }
  }
}

If there is one more property after ['%','-','+'] in above case, same process continues..

var v = {}/* Object of above */, keys = Object.keys(v), simplifiedColumns = {};
for (var i = 0, l = keys.length; i < l ; i++) {
        if (v[i] instanceof Array) {

        }else{
              simplifiedColumns[keys[i]] = simplifiedColumns[keys[i]] || {};
        }
}

Please suggest me to complete this logic.

like image 942
Exception Avatar asked Oct 21 '22 07:10

Exception


1 Answers

Here is an algorithm that works, but it will create only one object for i.e. x, y and z and refer to that same object.

Also, the following example assumes that the order of the keys (provided by Object.keys()) is the same as the order in which the object was defined. This will not always be the case, so the better solution would be to change your object to an array:

var obj = [
   {
        "key": "a",
        "value": 1
   },
   {
        "key": "b",
        "value": ["x","y","z"]
   },
   {
        "key": "c",
        "value": [0,1,3]
   },
   {
        "key": "d",
        "value": ['%','-','+']
   }
];

But anyway, here is the algorithm using the original object notation:

var obj = {
   a : 1,
   b : ["x","y","z"],
   c : [0,1,3],
   d : ['%','-','+']
};

var keys = Object.keys(obj);

//set tempObj to the last array
var tempObj = obj[keys[keys.length - 1]];

//traverse the rest of the keys backwards
for (var i = keys.length - 2; i >= 0; i--) {
    var key = keys[i];

    //create new empty object
    var newObj = {};

    //append "tempObj" to that object and using the keys that are in the current array
    //or if the property isn't an array, use the property itself as key
    if (Array.isArray(obj[key])) {
        for (var k = 0; k < obj[key].length; k++) {
            newObj[obj[key][k]] = tempObj;
        }
    } else {
        newObj[obj[key]] = tempObj;
    }
    //override tempObj with the new created object
    tempObj = newObj;
}

FIDDLE

Btw, if you need separate, independent objects, you could change the line

newObj[obj[key]] = tempObj;

to something like

newObj[obj[key]] = copyObject(tempObj);

where copyObject is a function that creates a deep copy of an object. But I guess in that case, the performance will drop drastically since you are copying the same objects over and over again.

like image 116
basilikum Avatar answered Oct 27 '22 11:10

basilikum