Skip to content
Advertisement

Iterate over a list of values using javascript

I am looking to iterate over a list of values using javascript.

I have a list like this

Label: A    Value:  Test    Count: 4
Label: B    Value:  Test2   Count: 2
Label: C    Value:  Test3   Count: 4
Label: D    Value:  Test4   Count: 1
Label: C    Value:  Test5   Count: 1

My goal is to pass each row into different functions based on the label. I am trying to figure out if a multidimensional array is the best way to go.

Advertisement

Answer

var list = [
   {"Label": "A", "value": "Test", "Count": 4},
   {"Label": "B", "value": "Test2", "Count": 2},
   {"Label": "C", "value": "Test3", "Count": 4},
   {"Label": "D", "value": "Test4", "Count": 1},
   {"Label": "C", "value": "Test5", "Count": 1}
]

for(var i = 0, size = list.length; i < size ; i++){
   var item = list[i];
   if(matchesLabel(item)){
      someFunction(item);
   } 
}

You get to define the matchesLabel function, it should return true if the item needs to be passed to your function.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement