1

Hi have this array:

   [ {
        "Navn": "Long Island Iced Tea",
        "Nummer": "2",
        "Glas i ml": "250",
        "Instruktioner": "",
        "a": "Hæld is i glasset",
        "b": "pynt med en skive lime",
        "Ingredienser": [
            {
                "Type": "Spiritus",
                "Del1": [
                    {
                        "Cointreau": 20
                    }
                ],
                "Del2": [
                    {
                        "Gin": 20
                    }
                ],
                "Del3": [
                    {
                        "Rom_Lys": 20
                    }
                ],
                "Del4": [
                    {
                        "Tequila": 20
                    }
                ],
                "Del5": [
                    {
                        "Vodka": 20
                    }
                ]
            },
            {
                "Type": "Vand/Juice",
                "Del1": [
                    {
                        "Cola": 40
                    }
                ],
                "Del2": [
                    {
                        "Sprite": 20
                    }
                ]
            },
            {
                "Type": "Mixer",
                "Del1": [
                    {
                        "Lime_Sirup": 20
                    }
                ]
            }
        ]
    }]

Its for a Cocktailmachine.

And i want to filter it by "Del1" searching for (example) "Rom_Lys" & "Cola" & "Vodka", and then output a new array with these specifications.

I tried searching the forums, but can't seem to find something useful. Played around with filter and includes, but cant come up with anything useful.

Thx!

2 Answers 2

2

If you want to get items which are contains Cola, then you can use filter and some methods:

const filterWord = 'Cola';

const result = sampleData.filter(s => 
    s.Ingredienser.some(s => 
        s.Del1.some( e=> e[filterWord])));

console.log(result);

An example:

let sampleData = [
{
    "Navn": "Rom & Cola/ Cuba Libre",
    "Nummer": "0",
    "Glas i ml": "200",
    "Instruktioner": "",
    "a": "Hæld is i glasset",
    "b": "pynt med en skive citron",
    "Ingredienser": [
        {
            "Type": "Spiritus",
            "Del1": [
                {
                    "Rom_Lys": 40
                }
            ]
        },
        {
            "Type": "Vand/Juice",
            "Del1": [
                {
                    "Cola": 100
                }
            ]
        },
        {
            "Type": "Mixer",
            "Del1": [
                {}
            ]
        }
    ]
},
{
    "Navn": "Isbjørn",
    "Nummer": "1",
    "Glas i ml": "200",
    "Instruktioner": "",
    "a": "Hæld is i glasset",
    "b": "pynt med en skive citron",
    "Ingredienser": [
        {
            "Type": "Spiritus",
            "Del1": [
                {
                    "Vodka": 30
                }
            ]
        },
        {
            "Type": "Vand/Juice",
            "Del1": [
                {
                    "Sprite": 60
                }
            ]
        },
        {
            "Type": "Mixer",
            "Del1": [
                {
                    "Blå_Sirup": 30
                }
            ]
        }
    ]
}];


const filterWord = 'Cola';
const result = sampleData.filter(s => s.Ingredienser.some(s => s.Del1.some( e=> e[filterWord])));
console.log(result);

UPDATE:

If you want to check multiple key, then you can use hasOwnProperty method which checks whether the object contains desired key:

const filters = ['Cointreau', 'Gin', 'Rom_Lys'];
const result = sampleData.filter(s =>
    s.Ingredienser.some(ingred => {
        return Object.keys(ingred).some(k=> {
            if (Array.isArray(ingred[k])) {
                return ingred[k].some(s=> filters.some(f=> {
                    return s.hasOwnProperty(f);
                }))
            }
        });
    }
));

And the example:

let sampleData = [ {
"Navn": "Long Island Iced Tea",
"Nummer": "2",
"Glas i ml": "250",
"Instruktioner": "",
"a": "Hæld is i glasset",
"b": "pynt med en skive lime",
"Ingredienser": [
    {
        "Type": "Spiritus",
        "Del1": [
            {
                "Cointreau": 20
            }
        ],
        "Del2": [
            {
                "Gin": 20
            }
        ],
        "Del3": [
            {
                "Rom_Lys": 20
            }
        ],
        "Del4": [
            {
                "Tequila": 20
            }
        ],
        "Del5": [
            {
                "Vodka": 20
            }
        ]
    },
    {
        "Type": "Vand/Juice",
        "Del1": [
            {
                "Cola": 40
            }
        ],
        "Del2": [
            {
                "Sprite": 20
            }
        ]
    },
    {
        "Type": "Mixer",
        "Del1": [
            {
                "Lime_Sirup": 20
            }
        ]
    }
]
}];

const filters = ['Cointreau', 'Gin', 'Rom_Lys'];
const result = sampleData.filter(s =>
s.Ingredienser.some(ingred => {
    return Object.keys(ingred).some(k=> {
        if (Array.isArray(ingred[k])) {
            return ingred[k].some(s=> filters.some(f=> {
                return s.hasOwnProperty(f);
            }))
        }
    });
}
));
console.log(result);

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this, this is great. I realised that that i need more than this.. What if i want to filter Del1, Del2, Del3, Del4 & Del5, and and have multiple conditions like Cola & Sprite ?? I have added another Array in my top post, with more info.
1

Try using following code to filter the array.

var filtered = arr.filter(function(unique) {
    for (var index = 0; index < unique.Ingredienser.length; index++) {
        if (unique.Ingredienser[index].Del1[0].hasOwnProperty(selectedchoice)) {
            return true;
        }
    }
    return false;
});

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.