Currently, the only method I am aware of to convert Dictionary<K,V>
(such as from Json.NET) to Map<K,V>
in TypeScript involves the following steps:
const json = '{ "1": "one", "2": "two", "3": "three", "4": "four" }';
const dictionary = JSON.parse(json) as { [index: number]: string; };
const parsedMap = new Map<number, string>();
for (const i in dictionary) {
if (!dictionary.hasOwnProperty(i)) { continue; }
console.log(`dictionary[${i}]: ${dictionary[i]}`);
parsedMap.set(parseInt(i, 10), dictionary[i]);
}
console.log(`parsedMap.size: ${parsedMap.size}`);
console.log(`parsedMap.get(4): ${parsedMap.get(4)}`);
Are there any built-in methods or libraries that simplify this conversion process? Is the current approach too verbose?