Skip to the code
cf.
+25 XP

#740 List Changed Setting Paths

medium

An audit log records which settings changed, as dotted paths like editor.font.size or plugins.2.

Write changedPaths(before, after) for two JSON objects. Compare them recursively: when both values at a path are plain objects, compare each key in either one; when both are arrays, compare each index up to the longer length. A key or index that exists on only one side is reported at that path. Any other pair of values (including an object against an array) is reported if the two aren't deep-equal. Paths join keys and indexes with ..

Return the paths sorted in character-code order, as sort() sorts strings.

Example 1

Input: before = {"theme":"dark","editor":{"font":{"size":14,"family":"mono"},"tabs":2}}, after = {"theme":"dark","editor":{"font":{"size":16,"family":"mono"},"wrap":true}}
Output: ["editor.font.size","editor.tabs","editor.wrap"]

size changed, tabs was removed, and wrap was added.

Example 2

Input: before = {"plugins":["a","b"]}, after = {"plugins":["a","c","d"]}
Output: ["plugins.1","plugins.2"]

Index 1 changed and index 2 is new.

Constraints

  • Both inputs are JSON objects with at most 10^4 values in total.
  • Keys don't contain ..
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).