Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let swappingKV = function(values){
- let myData = [
- {first: 1},
- {second: 2},
- {third: 3},
- {fourth: 4},
- {fifth: 5}
- ];
- let newDictionary = {}; //We need this to store after we swap the object.
- for (let k in values){
- newDictionary[values[k]] = k; //Swapping.
- }
- console.log(myData) //Printing original data
- return newDictionary; //returning swapped and filled objects
- console.log(swappingKV({first:1, second:2, third:3, fourth:4, fifth:5}));
- // I also tried swapping using temp variable. My idea was to store the first object into the first temp variable
- // then swap them. That took longer than anticipated.
- -----------------------------------------------------------------------------------------------------------------------
- //I practiced similar concept in Python as well. I tried using same concept as above.
- newObject = {}
- originalObject = {'first': 1, 'second':2, 'third':3, 'fourth':4, 'fifth':5}
- for keys, values in originalObject.items(): //items() is in-built for dictionary in python
- newObject[values] = keys //Same concept as above. Swapping by setting values to keys.
- print newObject //Printing swapped object
- // There are several ways we could swap this. The other way I was thinking was we could directly write for-in
- // loop and declare value:key with items() function at the end. That is another quickest way I thought of doing it.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement