Code Lab

Required fields

EasyJSON and data4 min

An API returned an ad item, and the array required lists the fields it must have. Write a function missingFields(item, required) that returns an array of the fields from required that item does not have, in the order of required.

Examples

missingFields({ id: 1, price: 500 }, ['id', 'title', 'price'])

Returns [ 'title' ]

missingFields({ id: 0, title: '' }, ['id', 'title'])

Returns []

A field is there even when its value is empty.

Asked at Avito · Source

Loading the editor...
  1. InputmissingFields({ id: 1, price: 500 }, [ 'id', 'title', 'price' ])
    Expected[ 'title' ]
  2. InputmissingFields({ id: 0, title: '' }, [ 'id', 'title' ])
    Expected[]
  3. InputmissingFields({}, [ 'id', 'title' ])
    Expected[ 'id', 'title' ]

Hidden tests run on Submit.