Javascript - 从对象数组中获取下一个字母

标签 javascript jquery json geojson

所以我有一个对象数组。实际上,这些对象遵循 GeoJSON 规范,因此请记住这一点。在“属性”对象中,存在“名称”属性。对于每个不同的功能,该名称将为 A、B、C...blah...Z、AA、AB 等。请参阅 JSON 示例(我确实删除了一些对这个问题不重要的其他内容,例如几何图形以及其他不重要的内容...):

{
    "features" : [{
            "properties" : {
                "name" : "A",
                "description" : null,
            },
            "type" : "Feature"
        }, {
            "properties" : {
                "name" : "B",
                "description" : null,
            },
            "type" : "Feature"
        }, {
            "properties" : {
                "name" : "C",
                "description" : null,
            },
            "type" : "Feature"
        }
    ],
    "type" : "FeatureCollection"
}

我想要做的是找到这个特征数组中的 MAX 字母,以返回该系列中的下一个特征。在此示例中,“C”将被视为最大值,因此我应该需要返回“D”的值。如果我有 AA,那将被视为 MAX,并且它将返回“AB”。如果最大值恰好是“Z”,我想返回“AA”值。
可以忽略小写字母的使用,只使用 26 个大写英文字母。没有其他字符。

我相信我可以通过使用 javascript CharCodeAt(index) 以及应用 Math.max、添加 + 1,然后转换回它的 ascii 字符表示来解决这个问题...但我在解决这个问题时遇到了麻烦一起在一个工作函数中循环遍历所有这些东西。

帮助将不胜感激!

更新: 我用下面的代码部分地工作了它。然而,还没有完全弄清楚如果它从 Z 绕到 AA,如何让它工作。或者如果发现MAX是AF,则返回AG。 AZ必须归还BA。

String.fromCharCode(Math.max.apply(Math,someObject.features.map(function(o){return o.properties.name.charCodeAt(0);})) + 1)

其他已知规则:

  • 上限可以是 ZZ - 我不太可能需要回滚到 AAA
  • 最大字符并不总是数组中的最后一个,所以不能 只需获取数组的最后一个特征即可。

最佳答案

使用Array.sortString.fromCharCodeString.charCodeAt函数的解决方案:

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'

关于Javascript - 从对象数组中获取下一个字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38129655/

相关文章:

javascript - 这里是 Node.js 的新手。如何正确从 subreddit 顶部获取 JSON?

javascript - WordPress 页面内的 p5js 草图

javascript - 我可以强制浏览器在 javascript 执行期间呈现 DOM 更改吗

javascript - 使用 jQuery on() 时取消 Ajax 之后的事件处理

javascript - 如果 Facebook 评论为 0,则 Conceal div

java - 外部服务的创建 API。发送二进制数据

javascript - 注入(inject) Controller 和模拟 AngularJS 服务

javascript - 使用 jQuery 转换超链接标记时忽略图像标签

javascript - 扩展javascript按键功能

javascript - 如何循环 JSON post 响应并将其数据打印到图像 div 中?