I've got a JSON objects array, from which I want to get a couple of random values. I did wrote some code myself, and it works in the end, but it's to ugly to even show.
So that's why I started this question. What should be good/nice ways to code the following situation?
We've got an JSON array like this: (it's longer actually, but just a couple as example)
"features" : [
{
"attributes" : {
"OBJECTID" : 6,
"Type" : "Gebied"
}
},
{
"attributes" : {
"OBJECTID" : 70,
"Type" : "Water"
}
},
{
"attributes" : {
"OBJECTID" : 80,
"Type" : "Water"
}
},
{
"attributes" : {
"OBJECTID" : 91,
"Type" : "Land"
}
},
{
"attributes" : {
"OBJECTID" : 66,
"Type" : "Gebied"
}
},
{
"attributes" : {
"OBJECTID" : 78,
"Type" : "Land"
}
}
]
From that array we want to create a new simple array, which contains, for example:
- 2 features with
"type" = "Gebied"
- 1 feature with
"Type" = "Land"
Actually the number of features to select, which in this example are 1 and 2, can differ (up to 20 for one single type).
And most importantly, those features should be selected random.
I'm curious which approaches you guys would take and hopefully it helps to create a real nice block of code to do this, instead of the almost 100 rules of code I used now (and not even finished).