Code Lab

Mode entretien. Minuteur de 4 min, sans indices ni autocomplétion

Field from a response

FacileJSON and data4 min

A test got the body of an API response as a JSON string. The product is in the field data.product. Write a function getProduct(body) that returns an object with only the name and price of that product.

Exemples

getProduct('{"status":200,"data":{"product":{"id":7,"name":"Phone","price":300}}}')

Renvoie { name: 'Phone', price: 300 }

The `id` field is not in the result.

Source

Chargement de l'éditeur...
  1. EntréegetProduct('{"status":200,"data":{"product":{"id":7,"name":"Phone","price":300}}}')
    Attendu{ name: 'Phone', price: 300 }
  2. EntréegetProduct('{"status":200,"data":{"product":{"id":12,"name":"Laptop","price":1500,"stock":4}}}')
    Attendu{ name: 'Laptop', price: 1500 }

Les tests cachés se lancent à la soumission