-3

I am pretty new to JavaScript. I have a nested object and I want to be able to reorder the key/value pairs. I tried looking for a solution at a lot of places online but mostly found answers to sort a 2-D array, nothing with this complexity level.
My object looks something like this:

id:{  
    Category1 :  {  
        a : {count:1, volume:5}  
        b : {count:2, volume:10}  
    }
    Category2: {  
        a : {count:4, volume:8}  
        b : {count:10, volume:4}  
    }  
}

and I want to sort it on the basis of the name instead of category. Intedend result:

id:{  
    a : {  
        Category1 : {count:1, volume:5}  
        Category2 : {count:4, volume:8} 
    }   
    b: {  
        Category1 : {count:2, volume:10}  
        Category2 : {count:10, volume:4} 
    }
 }
Garima
  • 13
  • 4
  • 2
    show us something you have tried? – kukkuz Aug 17 '17 at 07:00
  • Objects don't have 'order,' So, this question is a bit confusing... so given that objects don't have intrinsic ordering like arrays - you want to nest it differently? Is this a class question? PS syntax consistency is important. – sheriffderek Aug 17 '17 at 07:02
  • @sheriffderek From the example, he obviously means nesting order, not linear order. – Barmar Aug 17 '17 at 07:03
  • 1
    I don't think the question is marked duplicate correctly – marvel308 Aug 17 '17 at 07:09
  • This is not a duplicate - of the referenced post @Barmar - I don't think that it is obvious, but something is... – sheriffderek Aug 17 '17 at 07:14
  • @Garima - try and work something out in a jsFiddle like this: https://jsfiddle.net/sheriffderek/u03trgc6/ – sheriffderek Aug 17 '17 at 07:15
  • @sheriffderek I thought his essential problem was not knowing how to create keys dynamically, so I marked it as a dup of that. – Barmar Aug 17 '17 at 07:15
  • You either didn't think enough - or thought too much... either way, the OP's problem is - they have a practice problem and they don't know JavaScript well. The problem is to get this one array to the format of the other... so, what is the best way to make this questions a teachable moment for the next person in this class who posts this question? Or you can just give an answer with no explanation like marvel308 did. – sheriffderek Aug 17 '17 at 07:18
  • added an explanation :P – marvel308 Aug 17 '17 at 07:30

1 Answers1

1

You can do it using

let id = {  
    Category1 :  {  
        a : {count:1, volume:5},  
        b : {count:2, volume:10}  
    },
    Category2: {  
        a : {count:4, volume:8},  
        b : {count:10, volume:4}  
    }  
}

for(obj in id){
    for(obj2 in id[obj]){
        id[obj2] = id[obj2] || {};
        id[obj2][obj] = id[obj][obj2];
    }
    delete id[obj];
}

console.log(id);

in this code

for(obj in id)

traverses all properties of id so obj would be in the set {"Category1", "Category2"} and id[obj] would refer to the sub object

for(obj2 in id[obj])

traverses the property of the sub object and so obj2 would be in the set {"a", "b"}

id[obj2] = id[obj2] || {};

initialises the new property of id if it doesn't already exist i.e id["a"] and id["b"]

id[obj2][obj] = id[obj][obj2];

assigns the sub sub object ({count:1, volume:5}) to the new property within the sub property obj

delete id[obj];

lastly delete the current property since it is not of use anymore

marvel308
  • 10,288
  • 1
  • 21
  • 32