Iterate through array of objects to match two conditions

Given an array of objects like this:

let kandidaten = [{
        name: "Pfennig",
        branche: "Banken",
        thema: "Parkplätze"
    },
    {
        name: "Heller",
        branche: "Banken",
        thema: "Gebühren"
    },
    {
        name: "Wolf",
        branche: "Beratung",
        thema: "Ausbildung"
    },
    {
        name: "Schaf",
        branche: "Hotel",
        thema: "Ausbildung"
    }
]

I want to check this array of objects via an user-input which asks for two condition: “branche” and “thema”. If the object matches both conditions it should be displayed. How should I do this?

This is the code, to get the input from the HTML:

const selectedBranche = document.getElementById("branche").value;
const selectedThema = document.getElementById("thema").value;
function getSelectValue() {
    document.getElementById("outBranche").innerHTML = selectedBranche;
    document.getElementById("outThema").innerHTML = selectedThema;
}

For checking the object-array I did this:

function branchenCheck(selectedBranche, selectedThema) {
    for (var i = 0; i < kandidaten.length; i++) {
        for (key in kandidaten[i]) {
            if (kandidaten[i][key].indexOf(selectedBranche) != -1) {
                resultBranche.push(kandidaten[i]);
            }
        }
    }
    console.log(resultBranche)

It works, I get all objects which match the query “Branche”.

But I don’t know, how the code should look, if two queries should be matched. “&&” does not work. Like in;

function branchenCheck(selectedBranche, selectedThema) {
    for (var i = 0; i < kandidaten.length; i++) {
        for (key in kandidaten[i]) {
            if (kandidaten[i][key].indexOf(selectedBranche) && kandidaten[i][key].indexOf(selectedThema)  != -1) {
                resultBranche.push(kandidaten[i]);
            }
        }
    }

Maybe it is possible to create a filter object. Like

let filter = {
    branche: selectedBranche,
    thema: selectedThema
}

But I does not work too. Maybe because “selectedBranche” and “selectedThema” are variables?

Thank you in advance!

if (kandidaten[i][key].indexOf(selectedBranche) && kandidaten[i][key].indexOf(selectedThema) != -1)

That isn’t quite how the and works. If you want to check if the index of selectedBranche is not equal to -1, you’ll need to perform the comparison in both expressions around the &&:

if (kandidaten[i][key].indexOf(selectedBranche) != -1 && kandidaten[i][key].indexOf(selectedThema)  != -1)

The way you have it in your code above, the left expression is checking for truthyness based on the index integer being 0 (false) or not 0 (true), while the right operand is checking truthyness depending on whether or not the index is not equal to -1

1 Like

Hi @cmccormack thanks! This makes sense to me. But when given:

function branchenCheck(selectedBranche, selectedThema) {
    for (var i = 0; i < kandidaten.length; i++) {
        for (key in kandidaten[i]) {
            if (kandidaten[i][key].indexOf(selectedBranche) != -1 && kandidaten[i][key].indexOf(selectedThema) != -1) {
                resultBranche.push(kandidaten[i]);
            }
        }
    }
    console.log(resultBranche)

}

I get an empty array from console. But my query matches some of the “Kandidaten”-Objects.

let kandidaten = [{
        name: "Pfennig",
        branche: "Banken",
        thema: "Parkplätze"
    },
    {
        name: "Heller",
        branche: "Banken",
        thema: "Gebühren"
    },
    {
        name: "Wolf",
        branche: "Beratung",
        thema: "Ausbildung"
    },
    {
        name: "Schaf",
        branche: "Hotel",
        thema: "Ausbildung"
    }
]

So

selectedBranche matches Beratung
selectedThema matches Ausbildung
So I should get the object with "name: Wolf"

Instead I get an empty array…

You are trying to compare the value of the same key of the same object to two different strings at the same time. And want to proceed only if it’s equal to two different strings, which is never going to happen. Rethink your inner loop.