So I have an array of objects. Really the objects follow the GeoJSON specification, so keep that in mind. Within the "properties" objects, exists a proerty of "name". This name will be A, B, C... blah ... Z, AA, AB, etc. etc. for each different feature. See JSON example (I did strip out some other things which weren't important to this question, such as the geometry and what not...):
{
"features" : [{
"properties" : {
"name" : "A",
"description" : null,
},
"type" : "Feature"
}, {
"properties" : {
"name" : "B",
"description" : null,
},
"type" : "Feature"
}, {
"properties" : {
"name" : "C",
"description" : null,
},
"type" : "Feature"
}
],
"type" : "FeatureCollection"
}
What I'd like to do is find the MAX letter within this array of features to return the next one in the series. In this example, 'C' would be considered the MAX, so I should need to return the value of 'D'. If I had AA, that would be considered the MAX, and it would return 'AB'. If the max value happened to be a 'Z', I'd like to return a value of 'AA'.
Can ignore use of lowercase and only utilize 26, upper-cased english letters. No other characters.
I believe I could solve this with some usage of the javascript CharCodeAt(index) as well as applying Math.max, adding + 1, then converting back to it's ascii character represenation... but I'm having trouble bringing this together in a working function that loops through all this stuff.
Help would be appreciated!
Update: I got it partially working with the following code. However haven't quite figured out how to get it to work if it wraps around from Z to AA. Or if the MAX is found to be AF, then returning AG. AZ would have to return BA.
String.fromCharCode(Math.max.apply(Math,someObject.features.map(function(o){return o.properties.name.charCodeAt(0);})) + 1)
Other known rules:
- Upper limit can be ZZ - highly unlikely I'd need to wrap back to AAA
- The max character will not always be last in the array, so can't simply get the last feature of the array.