I want to ask for code review for my JavaScript implementation of Flatten_dictionary about this coding challenge for "Flatten a Dictionary" can be found at pramp:
Given a dictionary dict, write a function flattenDictionary that returns a flattened version of it .
If you’re using a compiled language such Java, C++, C#, Swift and Go, you may want to use a Map/Dictionary/Hash Table that maps strings (keys) to a generic type (e.g. Object in Java, AnyObject in Swift etc.) to allow nested dictionaries.
Example:
Input:
dict = { "Key1" : "1", "Key2" : { "a" : "2", "b" : "3", "c" : { "d" : "3", "e" : "1" } } }Output:
{ "Key1" : "1", "Key2.a" : "2", "Key2.b" : "3", "Key2.c.d" : "3", "Key2.c.e" : "1" }Important: when you concatenate keys, make sure to add the dot character between them. For instance concatenating Key2, c and d the result key would be Key2.c.d.
#implementation#1
function flattenDictionary(dict) {
//flattened dictionary = {}
//iterate through dictionary
//check for type of value for each key
//if value is another dictionary, i will recurivec callthis appending it to the dictionary
let flattened = {}
let keys = Object.keys(dict) //give me an array of keys
for (let i=0; i< keys.length; i++){
if (typeof dict[keys[i]] === "object") {
let flatObj = flattenDictionary(dict[keys[i]])
let newKeys = Object.keys(flatObj) //[a,b]
for (let j=0; j<newKeys.length; j++) {
if (newKeys[j] === "") {
flattened[keys[i]] = flatObj[newKeys[j]]
} else {
flattened[keys[i]+"."+newKeys[j]] = flatObj[newKeys[j]]
}
}
//get back to this
} else {
flattened[keys[i]] = dict[keys[i]]
}
}
return flattened
}
#implementation#2
function flattenDictionary(dict) {
// your code goes here
let results = {};
let currKey = "";
function flattenUtil(dict) {
// 1. 'Iterate' over the dictionary
// Keys will either be 'Key_' for outermost level
// or a letter/empty string for inner levels
// 2. If the value is an int/string, append onto output dictionary
// else if the value is a dictionary, recurse
// 3. Base case: dict empty, return
Object.keys(dict).forEach(key => {
if (typeof dict[key] === 'object') {
if (!currKey) {
currKey += key;
} else {
currKey += `.${key}`;
}
flattenUtil(dict[key]);
} else {
if (!currKey) {
results[`${key}`] = dict[key];
} else {
if (!key) {
results[`${currKey}`] = dict[key];
} else {
results[`${currKey}.${key}`] = dict[key];
}
}
}
});
}
flattenUtil(dict);
return results;
}