-3

I want to know how can we remove some objects from the array using its key values?

For example in this code I am returning only the recettes which have this name "Lait de coco" but how can I remove from the array only the recettes which contain this string?

let result = recettes.filter(elt => {
    return elt.ingredients.some(i => i.ingredient == "Lait de coco" )
})
console.log("result", result)

This is what I am trying but it is not working. Where could be the problem?

let result = recettes.filter(elt => {
    return elt.ingredients.some(i => i.ingredient !== "Lait de coco" )
})
console.log("result", result)

This is my Array

const recipes = [
    {
        "id": 1,
        "name" : "Limonade de Coco",
        "servings" : 1,
        "ingredients": [
            {
                "ingredient" : "Lait de coco",
                "quantity" : 400,
                "unit" : "ml"
            },
            {
                "ingredient" : "Jus de citron",
                "quantity" : 2
            },
            {
                "ingredient" : "Crème de coco",
                "quantity" : 2,
                "unit" : "cuillères à soupe"
            },
            {
                "ingredient" : "Sucre",
                "quantity" : 30,
                "unit" : "grammes"
            },
            {
                "ingredient": "Glaçons"
            }
        ],
        "time": 10,
        "description": "Mettre les glaçons à votre goût dans le blender, ajouter le lait, la crème de coco, le jus de 2 citrons et le sucre. Mixer jusqu'à avoir la consistence désirée",
        "appliance": "Blender",
        "ustensils": ["cuillère à soupe", "verres", "presse citron" ]
    },
    {
        "id": 2,
        "name" : "Poisson Cru à la tahitienne",
        "servings": 2,
        "ingredients": [
            {
                "ingredient" : "Thon Rouge (ou blanc)",
                "quantity" : 200,
                "unit" : "grammes"
            },
            {
                "ingredient" : "Concombre",
                "quantity" : 1
            },
            {
                "ingredient" : "Tomate",
                "quantity" : 2
            },
            {
                "ingredient" : "Carotte",
                "quantity" : 1
            },
            {
                "ingredient" : "Citron Vert",
                "quantity" : 5
            },
            {
                "ingredient" : "Lait de coco",
                "quantity" : 100,
                "unit" : "ml"
            }
        ],...
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
deveb2020
  • 101
  • 2
  • 9
  • Take a look at the javascript [filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) – dwb Feb 25 '21 at 11:27
  • 1
    Does this answer your question? [Remove array element based on object property](https://stackoverflow.com/questions/15287865/remove-array-element-based-on-object-property) – dwb Feb 25 '21 at 12:57
  • 1
    Please don't make more work for others by vandalizing your posts. – Ollie Feb 25 '21 at 17:36
  • mmm, so it doesn't work in SO, that is, if you don't get your answer in a short time (6 hours is nothing for SO since you can get answers in minutes, days, years or never) then you shouldn't publish a new post with the same question, what you have to do is improve and hope that other members of the community will help you. By creating new posts, you only get to discourage the community from trying to help you. If you want to attract more attention then you can give a bounty. Please read [ask] and check the [tour] – eyllanesc Feb 25 '21 at 17:46

1 Answers1

2

Using JavaScript's array.filter function, we can do something like this:

let result = arrayOfPeople.filter(person => person.city !== "NY")
dwb
  • 2,136
  • 13
  • 27