Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
Getting object data with key value in JavaScript

I have an object like this:

{
  "idMeal": "52795",
  "strMeal": "Chicken Handi",
  "strDrinkAlternate": null,
  "strCategory": "Chicken",
  "strArea": "Indian"
}

I am trying to get the value of other items(i.e. strMeal and strCategory) using the value of "idMeal". How can I do that?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

As far as i understood you want to filter an list of food objects, just put them in an array and filter like this:

[obj1, obj2, obj3].filter(o => o.idMeal="52795")
about 4 years ago · Juan Pablo Isaza Denunciar

0

First you should have stated what you tried. Then, you can use a simple condition:

let cat;
let area;
if (obj.idMeal === myValue) {
    cat = obj.strCategory;
    area = obj.strArea;
}

There are way more ways of doing this. Take a look at .map(), .filter() but those are for arrays.

about 4 years ago · Juan Pablo Isaza Denunciar

0

The easiest way to achieve this is to filter the data based on the key-value pair and then reduced the requested fields into a new object.

const mealData = [{
  "idMeal": "52795",
  "strMeal": "Chicken Handi",
  "strDrinkAlternate": null,
  "strCategory": "Chicken",
  "strArea": "Indian",
}];

const filterAndMap = (data, key, value, fields) =>
  data
    .filter((record) => record[key] === value)
    .map(record => fields.reduce((acc, field) =>
      ({ ...acc, [field]: record[field] }), {}));

const result = filterAndMap(mealData, 'idMeal', '52795', ['strMeal', 'strCategory']);

console.log(result);

Alternatively, you could pass a filter function and a mapper function:

const mealData = [{
  "idMeal": "52795",
  "strMeal": "Chicken Handi",
  "strDrinkAlternate": null,
  "strCategory": "Chicken",
  "strArea": "Indian",
}];

const filterAndMap = (data, filterFn, mapperFn) =>
  data.filter(filterFn).map(mapperFn);

const result = filterAndMap(
  mealData,
  ({ idMeal }) => idMeal === '52795',
  ({ strMeal, strCategory }) => ({ strMeal, strCategory }));

console.log(result);

In the example above, we can remove the filterAndMap function and just chain filter and map directly.

const mealData = [{
  "idMeal": "52795",
  "strMeal": "Chicken Handi",
  "strDrinkAlternate": null,
  "strCategory": "Chicken",
  "strArea": "Indian",
}];

const result = mealData
  .filter(({ idMeal }) => idMeal === '52795')
  .map(({ strMeal, strCategory }) => ({ strMeal, strCategory }));

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda