TransWikia.com

If div children don't have same text content then do action

Stack Overflow Asked by LucR on January 21, 2021

I’m trying to create DOM elements for an Ingredient filter bar, based on JSON file objects.

The problem is that the same ingredient can appear in several objects, and in that case it should only create the dom element once, not for each time the ingredient occures.

I’ve tried with childNode, value, innerHTML and !=== but can’t figure out the solution. It either creates no element at all, or all of them with duplicates.

Any ideas?

Here is a codePen to help : https://codepen.io/enukeron/pen/eYdgyzx

I also tried with an array to keep track of seen values at this codepen :
https://codepen.io/enukeron/pen/ExgZoLa

JS:

const ingredientDropdown = document.getElementById('ingredientDropdown');

for(var j = 0; j < IngredientList.length; j++) {
   if (ingredientDropdown.children.textContent !== IngredientList[j].ingredient) {
      var ingredientSearchItems = document.createElement("p");
      ingredientSearchItems.textContent = IngredientList[j].ingredient;
      ingredientDropdown.appendChild(ingredientSearchItems);
   }
}

The JSON file has this format :

    {
        "id": 49,
        "name": "Tropical smoothie",
        "servings": 4,
        "ingredients": [
            {
                "ingredient": "Bananas",
                "quantity": 2
            },
            {
                "ingredient": "Kiwis",
                "quantity": 3
            },
            {
                "ingredient": "Mango",
                "quantity": 1
            },
            {
                "ingredient": "Pineapple",
                "quantity": 4,
                "unit":"slices"
            },
            {
                "ingredient": "Honey",
                "quantity": 2,
                "unit": "tablespoons"
            }
        ],
        "time": 0,
        "description":"Chop the fruit. Liquefy in the blender. Chill. Serve",
        "appliance": "Blender",
        "ustensils":["couteau", "verres"]
    }, etc.....

The actual result is : enter image description here

The Expected Result is : enter image description here

One Answer

You could create a function like the following:

function filterIngredients(recipes){
  let return_arr = [];
  recipes.forEach((recipe, index, array)=>{
    let ingredients = recipe["ingredients"];
    ingredients.forEach((ingredient, index, inner_array)=>{
      if(!return_arr.includes(ingredient["ingredient"])){
        return_arr.push(ingredient["ingredient"]);
      }
    });
  });
  return return_arr;
}

And then call the function as follows:

var ingredients = filterIngredients(recipes);

You can then loop through ingredients and display them in the div as you want (hoping this is what you wanted in the first place). Here is a link to my pen where I implemented it: https://codepen.io/AnirudhMS/pen/MWjJQgg?editors=1010

Correct answer by Black Wind on January 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP