Code Lab

Modo entrevista. Temporizador de 4 min, sin pistas y sin autocompletado

Field from a response

FácilJSON 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.

Ejemplos

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

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

The `id` field is not in the result.

Fuente

Cargando el editor…
  1. EntradagetProduct('{"status":200,"data":{"product":{"id":7,"name":"Phone","price":300}}}')
    Esperado{ name: 'Phone', price: 300 }
  2. EntradagetProduct('{"status":200,"data":{"product":{"id":12,"name":"Laptop","price":1500,"stock":4}}}')
    Esperado{ name: 'Laptop', price: 1500 }

Las pruebas ocultas se ejecutan al enviar