1
- // The question is given an object / dictionary where the keys are strings,
2
- // and the values are either strings or another object / dictionary, can you
1
+ // The question is given an object / dictionary where the keys are strings,
2
+ // and the values are either strings or another object / dictionary, can you
3
3
// return a new object / dictionary with the keys flattened.
4
4
5
5
// For example, { a: "b", c: { d: "e", "f": {g: "e"} } }, would become { a: "b", "c.d": "e", c.f: "g" }.
6
- // Notice that this function will remove any of the nested objects / dictionaries,
6
+ // Notice that this function will remove any of the nested objects / dictionaries,
7
7
// and will combine the keys in the nested objects / dictionaries to be joined by a period.
8
8
9
9
function flatten ( dict ) {
10
10
// step through each property
11
11
// if string, continue?
12
- // if object, append to the parent and
12
+ // if object, append to the parent and
13
13
const flattenedDictionary = { } ;
14
-
15
- for ( const key in dict ) {
14
+
15
+ for ( const key in dict ) {
16
16
const value = dict [ key ] ;
17
17
const parent = key ;
18
18
if ( typeof value === 'object' ) {
@@ -38,4 +38,5 @@ function flattenHelper(parent, dict, flattenedDict) {
38
38
39
39
40
40
console . log ( flatten ( { a : "b" , c : { d : "e" } } ) ) ;
41
- console . log ( flatten ( { a : "b" , c : { d : "e" , f : { e :"g" } } } ) )
41
+ console . log ( flatten ( { a : "b" , c : { d : "e" , f : { e : "g" } } } ) )
42
+ console . log ( flatten ( { a : "b" , c : { d : "e" , f : 'g' } } ) )
0 commit comments