How i can loop this object

How can I loop and display the object in side this array?
I want to retrieve …strIngredient1 -strIngredient20 …and skip null value


  1. [{…}]

  2. 0:

1. dateModified: "2017-09-02 18:37:54"
2. idDrink: "17216"
3. strAlcoholic: "Alcoholic"
4. strCategory: "Ordinary Drink"
5. strCreativeCommonsConfirmed: "No"
6. strDrink: "Tommy's Margarita"
7. strDrinkAlternate: null
8. strDrinkThumb: "https://www.thecocktaildb.com/images/media/drink/loezxn1504373874.jpg"
9. strGlass: "Old-Fashioned glass"
10. strIBA: "New Era Drinks"
11. strImageAttribution: null
12. strImageSource: null
13. strIngredient1: "Tequila"
14. strIngredient2: "Lime Juice"
15. strIngredient3: "Agave syrup"
16. strIngredient4: null
17. strIngredient5: null
18. strIngredient6: null
19. strIngredient7: null
20. strIngredient8: null
21. strIngredient9: null
22. strIngredient10: null
23. strIngredient11: null
24. strIngredient12: null
25. strIngredient13: null
26. strIngredient14: null
27. strIngredient15: null
28. strInstructions: "Shake and strain into a chilled cocktail glass."
29. strInstructionsDE: "Schütteln und in ein gekühltes Coc

Perhaps:

  1. pull out the keys into an array using Object.keys(...),
  2. Array.filter(...) the list to strings containing the “Ingredient” string that also represent non-null values in the array,
  3. Array.map(...) those values, returning the value from the original object for each key,
  4. Finally, return the result of that last step.

That’s the thought process I’d follow, in planning this one out. Hope that helps!

thank you very much. I will try now.

i tired this and stuck on how to get value after filtered

let objData = Object.keys(details[0]);
const filterItems = (arr, query) => {
return arr.filter(function (el) {
return el.toLowerCase().indexOf(query.toLowerCase()) !== -1;
});
};
console.log(filterItems(objData, “strIngredient”));
const testFilter = filterItems(objData, “strIngredient”);
console.log(testFilter);

Result:
(15) [“strIngredient1”, “strIngredient2”, “strIngredient3”, “strIngredient4”, “strIngredient5”, “strIngredient6”, “strIngredient7”, “strIngredient8”, “strIngredient9”, “strIngredient10”, “strIngredient11”, “strIngredient12”, “strIngredient13”, “strIngredient14”, “strIngredient15”]

then i don´t know how to use map to return value , yes i have tried , but it´s not work…(i am a beginer)

So at that point, you have a list of field names, and you want to find the fields in details[0] first, so maybe

// details[0] is your object, and we can
// use bracket notation to get the value.
const fields = testFilter.map(field=> details[0][field] );

And still, you will need to filter the fields for empty values.

1 Like

thank you again. I get it now. :pray:

1 Like