I have a data structure like this :
[
{ "key" : { "subkey" : "red", "value" : 1 } },
{ "key" : { "subkey" : "red", "value" : 2 } },
{ "key" : { "subkey" : "blue", "value" : 1 } },
{ "key" : { "subkey" : "yellow", "value" : 3 } },
{ "key" : { "subkey" : "blue", "value" : 5 } },
{ "key" : { "subkey" : "blue", "value" : 8 } },
{ "key" : { "subkey" : "red", "value" : 2 } },
{ "key" : { "subkey" : "red", "value" : 3 } },
{ "key" : { "subkey" : "red", "value" : 6 } },
]
The idea is I would like to iterate through it and when at least 2 "subkeys" are the same, fire off some_event(), which would add up the values from those consecutive objects, until it hits a different "subkey" again.
For example, the first and second dicts should fire off some_event() with values 2+1 added. Then nothing happens with the third (blue) nor fourth (yellow) lines, fifth and sixth (blue) fire off some_event() with values 5+8, etc.
thanks!
1 Answer