Skip to the code
cf.
+10 XP

#710 Patch Changed Props

easy

When a component re-renders with the same element type, the framework only touches the attributes that changed.

Write diffProps(oldProps, newProps) that returns { set, remove }. set is an object with every key whose value in newProps is new or different from oldProps, mapped to its new value. remove lists the keys that are in oldProps but not in newProps, sorted alphabetically (by character code).

Values are strings, numbers, or booleans, compared with ===.

Example 1

Input: oldProps = {"class":"btn","disabled":true,"title":"Save"}, newProps = {"class":"btn primary","title":"Save"}
Output: {"set":{"class":"btn primary"},"remove":["disabled"]}

class changed, title didn't, and disabled is gone.

Example 2

Input: oldProps = {}, newProps = {"id":"x","tabIndex":0}
Output: {"set":{"id":"x","tabIndex":0},"remove":[]}

Everything is new.

Constraints

  • Each object has 0 to 100 keys.
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).