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

时间:2016-06-30 17:23:22

标签: javascript jquery json geojson

所以我有一个对象数组。对象真的遵循GeoJSON规范,所以请记住这一点。在“属性”对象中,存在一个“名称”的元素。对于每个不同的特征,该名称将是A,B,C ......等等...... 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'将被视为MAX,所以我需要返回'D'的值。如果我有AA,那将被视为MAX,它将返回'AB'。如果最大值恰好是'Z',我想返回'AA'的值 可以忽略使用小写,只使用26,大写英文字母。没有其他角色。

我相信我可以通过使用javascript CharCodeAt(索引)以及应用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
  • 最大字符不会总是在数组中的最后一个,所以不能 只需获取数组的最后一个功能。

4 个答案:

答案 0 :(得分:1)

使用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'

答案 1 :(得分:0)

您可以找到MAX字符串:

var maxName = null,
    obj = null,
    name = null;
for(var idx = 0; idx < features.length; ++idx){
    obj = features[idx];
    name = obj.properties.name;
    if(maxName == null || name.length > maxName.length || 
        (name.length == maxName.length && name > maxName)
    ){
        maxName = name;
    }
}

我仍然在努力获得下一个名字。

答案 2 :(得分:0)

我认为你想排序,实际上JS有一个比较字符串的好功能。但是,它会返回("AA" > "Z") === true,因此您也需要考虑长度。

我认为这很有效。

function sortName(a, b){
    a = a.properties.name;
    b = b.properties.name;
    if(a.length>b.length){
        return 1;
    }
    if(a > b){
        return 1;
    }
    return -1;
}

console.log(someObject.features.sort(sortName)[0]. properties.name);

答案 3 :(得分:0)

您可以使用

执行此操作
var obj = {
         "features": [{
             "properties": {
                 "name": "A",
                 "description": null,
             },
             "type": "Feature"
         }, {
             "properties": {
                 "name": "B",
                 "description": null,
             },
             "type": "Feature"
         }, {
             "properties": {
                 "name": "C",
                 "description": null,
             },
             "type": "Feature"
         }],
         "type": "FeatureCollection"
     };


     var largest = Math.max.apply(Math, findProp(obj.features, "name"));
     console.log(changeToStr(largest + 1));

当findProp获取属性值数组时,changeToStr将number转换为string,changeToNum将number转换为String。

function changeToNum(val) {
         var base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
             i, j, result = 0;

         for (i = 0, j = val.length - 1; i < val.length; i += 1, j -= 1) {
             result += Math.pow(base.length, j) * (base.indexOf(val[i]) + 1);
         }
         return result;
     };


     function changeToStr(number) {
         var baseChar = ("A").charCodeAt(0),
             letters = "";

         do {
             number -= 1;
             letters = String.fromCharCode(baseChar + (number % 26)) + letters;
             number = (number / 26) >> 0;
         } while (number > 0);

         return letters;
     }

     function findProp(obj, key, out) {
         var i,
             proto = Object.prototype,
             ts = proto.toString,
             hasOwn = proto.hasOwnProperty.bind(obj);

         if ('[object Array]' !== ts.call(out)) out = [];

         for (i in obj) {
             if (hasOwn(i)) {
                 if (i === key) {
                     out.push(changeToNum(obj[i]));
                 } else if ('[object Array]' === ts.call(obj[i]) || '[object Object]' === ts.call(obj[i])) {
                     findProp(obj[i], key, out);
                 }
             }
         }

         return out;
     }

请在此处查看工作Fiddle