'Store value from object with key starting with same character

I have an object in react, and I would like to store the values that start with the same string in the key and that is not equal to an empty string. For example I would like to have another array with all the value from strIngredient from 1 to 15. I tried using Object.key(obj).find linked to startsWith but I lost myself on the track.

[
        {
            "idMeal": "52904",
            "strMeal": "Beef Bourguignon",
            "strDrinkAlternate": null,
            "strCategory": "Beef",
            "strArea": "French",
            "strTags": null,
            "strYoutube": "https://www.youtube.com/watch?v=SQnr4Z-7rok",
            "strIngredient1": "Goose Fat",
            "strIngredient2": "Beef Shin",
            "strIngredient3": "Bacon",
            "strIngredient4": "Challots",
            "strIngredient5": "Chestnut Mushroom",
            "strIngredient6": "Garlic Clove",
            "strIngredient7": "Bouquet Garni",
            "strIngredient8": "Tomato Puree",
            "strIngredient9": "Red Wine",
            "strIngredient10": "Celeriac",
            "strIngredient11": "Olive Oil",
            "strIngredient12": "Thyme",
            "strIngredient13": "Rosemary",
            "strIngredient14": "Bay Leaf",
            "strIngredient15": "Cardamom",
            "strIngredient16": "",
            "strIngredient17": "",
            "strIngredient18": "",
            "strIngredient19": "",
            "strIngredient20": "",
            "strMeasure1": "3 tsp",
            "strMeasure2": "600g",
            "strMeasure3": "100g ",
            "strMeasure4": "350g",
            "strMeasure5": "250g",
            "strMeasure6": "2 sliced",
            "strMeasure7": "1",
            "strMeasure8": "1 tbs",
            "strMeasure9": "750 ml ",
            "strMeasure10": "600g",
            "strMeasure11": "2 tbs",
            "strMeasure12": "sprigs of fresh",
            "strMeasure13": "sprigs of fresh",
            "strMeasure14": "2",
            "strMeasure15": "4",
            "strMeasure16": "",
            "strMeasure17": "",
        }
    ]


Solution 1:[1]

Hmmm, I'm not entirely sure what you mean by:

start with the same string in the key and that is not equal to an empty string

Here's some code that creates a Map using only the keys that start with 'str' (but strips off the 'str' prefix), and only contains the values that are non-null or non-empty strings; hopefully that will get you started!

return Object.keys(data).reduce((map, key) => {
  const value = data[key];
  if (!value) return map;

  // Find keys that start with 'str' and ignore any numbers at the end
  const match = /(?<=str).+?(?=[\d]+$|$)/.exec(key);
  if (!match) return map;

  const [keyTrimmed] = match;
  const existingValues = map.get(keyTrimmed) ?? [];

  return map.set(keyTrimmed, [...existingValues, value]);
}, new Map<string, string[]>());

See: CodeSandbox

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gerrod