Print Nested Key-Value Pairs
MediumP2Coding
obj is a nested object whose values are integers, strings, or further objects. Use recursion to list every leaf value together with its path.
Return an array of strings of the form "path: value", where path joins the keys from the root with .. List entries in the order the keys appear in the object (depth first). An empty nested object contributes nothing.
Example 1
- Input
- obj = {"a":1,"b":{"c":2,"d":{"e":"x"}}}
- Output
- ["a: 1","b.c: 2","b.d.e: x"]
- Why
- Keys are joined with dots in depth-first order.
Example 2
- Input
- obj = {}
- Output
- []
Example 3
- Input
- obj = {"k":"v"}
- Output
- ["k: v"]
Constraints
- Keys are non-empty strings that are not whole numbers and contain no
. - Leaf values are integers or strings; nesting depth is at most 50
- Total number of leaves is at most 1000
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 8. ⌘Enter runs, ⌘⇧Enter submits.