Swap key,value inside a node.js collections dictionary -
i using node.js collection module. http://www.collectionsjs.com/
i swap key value inside collections dictionary.
to illustrate, here original dictionary.
"use strict"; var dict = require("collections/dict"); var data_type = new dict( { "00": "data_0", "01": "data_1", "02": "data_2", });
after swapping, new dictionary this;
var data_type_swapped = new dict( { "data_0": "00", "data_1": "01", "data_2": "02", });
there no duplicate values in original dictionary.
i using node.js v6.
i not familiar library, skimmed documentation , came this:
let dict = require('collections/dict'); let d = new dict({ 'key1': 'value1', 'key2': 'value2' }); d.keys().foreach(key => { d.set(d.get(key), key); d.delete(key); // cause trouble if value same key });
it iterates through keys, adds new entry identified value of current key, sets value , deletes old key
Comments
Post a Comment