Search code examples
javascriptobjectif-statementfor-loopfor-in-loop

Iterate object and list all found properties that match input


Im trying to solve a javascript problem I have. I have a object with lots of properties. I want to iterate over the properties to find ALL entries of the input the user does. I've written a for..in-loop but its not giving me any info.

Any ideas?

var numbers = {
    "Group1": "3300",
    "Group2": "1200",
    "Group3": "5000"
};

function test(){

        var input = document.getElementById("inputTxt").value;

        for(var group in numbers){

             if(numbers[group] == input){
                 console.log(numbers[group])
             }
        }           
}

This code is logging nothing in my console. What A'm I doing wrong?

Thanks!


Solution

  • i've created a plnkr here,

    https://plnkr.co/edit/cpdipV2qxAUa4pHtdKqA?p=preview

    it seems to work

    for(var group in numbers){
    
                 if(numbers[group] == input){
                     console.log(numbers[group])
                 }
            }