dvsoukup dvsoukup - 5 months ago 8
jQuery Question

Javascript - Fetch the next letter from an array of objects

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.


Answer

The solution using Array.sort, String.fromCharCode and String.charCodeAt functions:

var someObject = {
    "features" : [{
            "properties" : { "name" : "AB", "description" : null},
            "type" : "Feature"
        }, {
            "properties" : {"name" : "B", "description" : null},
            "type" : "Feature"
        }, {
            "properties" : { "name" : "AF", "description" : null},
            "type" : "Feature"
        }
    ],
    "type" : "FeatureCollection"
};

function getNext(data) {
    data.features.sort(function(a,b){
        return a.properties.name.length - b.properties.name.length ||
                a.properties.name.localeCompare(b.properties.name);
    });

    var last = data.features[data.features.length - 1].properties.name;
    if (last.length === 1) {
        return (last === "Z")? "AA" : String.fromCharCode(last.charCodeAt(0) + 1);
    } else {
        if (last === "ZZ") return last;  // considering 'ZZ' as a limit
        if (last[1] !== "Z") {
            return last[0] + String.fromCharCode(last[1].charCodeAt(0) + 1);
        } else if (last[1] === "Z"){
            return  String.fromCharCode(last[0].charCodeAt(0) + 1) + "A";
        }
    }       
}

console.log(getNext(someObject));  // 'AG'
Comments