I am looking to iterate over a list of values using javascript.
I have a list like this
JavaScript
x
6
1
Label: A Value: Test Count: 4
2
Label: B Value: Test2 Count: 2
3
Label: C Value: Test3 Count: 4
4
Label: D Value: Test4 Count: 1
5
Label: C Value: Test5 Count: 1
6
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
JavaScript
1
15
15
1
var list = [
2
{"Label": "A", "value": "Test", "Count": 4},
3
{"Label": "B", "value": "Test2", "Count": 2},
4
{"Label": "C", "value": "Test3", "Count": 4},
5
{"Label": "D", "value": "Test4", "Count": 1},
6
{"Label": "C", "value": "Test5", "Count": 1}
7
]
8
9
for(var i = 0, size = list.length; i < size ; i++){
10
var item = list[i];
11
if(matchesLabel(item)){
12
someFunction(item);
13
}
14
}
15
You get to define the matchesLabel
function, it should return true if the item needs to be passed to your function.