0

I need to push in array data if exist in array? Example code:

  filterMealType(type){ 
        this.filteredRecipe.map(
          obj => { 
            if(obj.mealTypes.includes(type)){
              this.filterRecipeByMealType.push(obj) 
            }else {
              this.filterRecipeByMealType = []
            }
          }
        )
        console.log('current array ', this.filterRecipeByMealType)
      }

type as parameter is only number, 0 or 1 or 2 or 3. filteredRecipe is array:

[
   {
      "name":"Obroki",
      "description":"",
      "difficulty":4,
      "preparationTime":1920,
      "ingredients":[
         "dsad",
         "dasdas"
      ],
      "instructions":[
         "sad",
         "dsa"
      ],
      "pictureUrl":"https://www.url.com/images/cb51adad61a04936b831378865ad8bf6.jpg",
      "recepieType":"",
      "mealTypes":[
         0,
         2
      ],
      "id":281
   },
   {
      "name":"Testen",
      "description":"",
      "difficulty":3,
      "preparationTime":240,
      "ingredients":[
         "sestavino",
         "test"
      ],
      "instructions":[
         "inst"
      ],
      "pictureUrl":"https://www.url.com/images/aca27231f9af4ed0af5e1ec4af09861e.jpg",
      "recepieType":"",
      "mealTypes":[
         0,
         2
      ],
      "id":282
   },
   {
      "name":"Mleko",
      "description":"",
      "difficulty":2,
      "preparationTime":2580,
      "ingredients":[
         "ddd"
      ],
      "instructions":[
         "das",
         "dasd"
      ],
      "pictureUrl":"https://www.url.com/images/1fd0bd88175a4680ae9430579f1b781c.jpg",
      "recepieType":"",
      "mealTypes":[
         1
      ],
      "id":283
   },
   {
      "name":"test",
      "description":"",
      "difficulty":4,
      "preparationTime":240,
      "ingredients":[
         "jajca",
         "meso"
      ],
      "instructions":[
         "rf"
      ],
      "pictureUrl":"https://www.url.com/images/0fafcd552354468bbca3a78cb9796004.jpg",
      "recepieType":"",
      "mealTypes":[
         0,
         2
      ],
      "id":211
   }
]

I if obj.mealTypes includes any of number i need to push to array. Right now no work good. Now i only push one item...no work good. If you need i can share to stackblitz

2 Answers 2

1

Just remove your else condition and reset your array at the begining of your function

 filterMealType(type){ 
    this.filterRecipeByMealType = []
    this.filteredRecipe.map(
      obj => { 
        if(obj.mealTypes.includes(type)){
          this.filterRecipeByMealType.push(obj) 
        }
      }
    )
    console.log('current array ', this.filterRecipeByMealType)
  }
Sign up to request clarification or add additional context in comments.

2 Comments

Friend when i select another time second item, my array is not cleared
I edited my answer in the meantime, did you see it ? The reset at the begining of the function
0

Use the filter method on the filteredRecipe array to return a new array with only recipes that have the meal type you're looking for.

filterMealType(type) { 
  this.filterRecipeByMealType = this.filteredRecipe.filter(
    recipe => recipe.mealTypes.includes(type)
  );
  console.log('current array ', this.filterRecipeByMealType)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.