0

I have a set of properties inside of a javasript object.

    var leanToolButtonStatus = {
    FiveSStatus:0,
    SmallLotStatus:0,
    SmedStatus:0,
    QualityStatus:0,
    CellsStatus:0,
    CrossTrainStatus:0,
    SelfDirectedStatus:0,
    PMStatus:0,
    VendorStatus:0,
    SmallPurchaseStatus:0,
    NewEquipmentStatus:0,
    MarketStatus:0,
    KanbanStatus:0
}

Now I want to run a for-loop to iterate over that so I try:

  function loopThroughObject(){
    var iterationCounter = 0;
    for(var x = 0; x<1; x++){
        for(var y = 0; y<6; y++){
        switch(leanToolButtonStatus[iterationCounter]) {
            case 0:
                break;
            case 1:
                break;
            case 2:
                break;
            }
        }
        iterationCounter++;
    }
}

However I get the following error:

Uncaught SyntaxError: Unexpected number.

Any ideas where I am going wrong?

3 Answers 3

2

if the structure of your object is exactly like you added in your question, you wont need for loop, just access it as :

var leanToolButtonStatus = {
    FiveSStatus:0,
    SmallLotStatus:0,
    SmedStatus:0,
    QualityStatus:0,
    CellsStatus:0,
    CrossTrainStatus:0,
    SelfDirectedStatus:0,
    PMStatus:0,
    VendorStatus:0,
    SmallPurchaseStatus:0,
    NewEquipmentStatus:0,
    MarketStatus:0,
    KanbanStatus:0
};
console.log(leanToolButtonStatus["FiveSStatus"]);

if you dont know the key names. you can use for..in loop, as:

for(val in leanToolButtonStatus ) {
    console.log(leanToolButtonStatus[val]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I need to be able to iterate through a loop in order for some some drawing placement to be handled. So therefore I wouldn't be able to call just the key. Does that make sense?
@Tukajo in that case you can use for..in loop to iterate thru object key/values like the one added in answer
2

It should work with this codesnippet:

for(key in list)
{
 var elementOfList = list[key];
}

key could be 'FiveSStatus' and elementOfList 0

Comments

1
#1
for (var key in leanToolButtonStatus) {
   leanToolButtonStatus[key]
}

#2
Object.keys(leanToolButtonStatus).forEach(function (key) {
    leanToolButtonStatus[key]
});

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.