Skip to content
Exercise · Map, Set and JSON · lesson 9 of 9

Exercise: what JSON loses

Write a function function lostKeys(obj) that returns an array of the keys of the object obj that disappear when it is saved as JSON. These are the keys the object will not have after obj is turned into a JSON string and read back. The keys follow the same order as in obj.

  • lostKeys({ id: 7, note: undefined }) → ["note"]
  • lostKeys({ price: NaN, size: Infinity }) → []
  • lostKeys({}) → []

Constraints: obj has 0 to 5 properties and no nested objects, and the values are strings, numbers (NaN and Infinity included), true, false, null, undefined or functions; if nothing disappears, the result is [].

Do not add extra imports unless the task says so explicitly.

lost-keys.js — javascript
javascript · 4 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way