Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!