Javascript - 从对象数组中获取下一个字母
Javascript - Fetch the next letter from an array of objects
所以我有一组对象。实际上,对象遵循 GeoJSON 规范,因此请记住这一点。在 "properties" 个对象中,存在一个 "name" 的属性。对于每个不同的功能,此名称将是 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 字母到 return 系列中的下一个。在此示例中,'C' 将被视为 MAX,因此我需要 return 'D' 的值。如果我有 AA,那将被认为是 MAX,它会 return 'AB'。如果最大值恰好是 'Z',我想 return 一个 'AA'.
的值
可以忽略小写,只使用26个大写英文字母。没有其他字符。
我相信我可以通过使用 javascript CharCodeAt(index) 以及应用 Math.max、添加 + 1,然后转换回它的 ascii 字符表示来解决这个问题...但我无法将它们整合到一个循环遍历所有这些东西的工作函数中。
不胜感激!
更新:
我使用以下代码部分地使用了它。但是,如果它从 Z 绕到 AA,还没有完全弄清楚如何让它工作。或者如果发现MAX是AF,那么returning AG。 AZ 必须 return BA。
String.fromCharCode(Math.max.apply(Math,someObject.features.map(function(o){return o.properties.name.charCodeAt(0);})) + 1)
其他已知规则:
- 上限可以是 ZZ - 我不太可能需要回到 AAA
- 最大字符不会总是在数组的最后,所以不能
只需获取数组的最后一个特征。
您可以通过以下方式找到 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;
}
}
不过,我仍在努力获取下一个名称。
我想你想排序,其实JS有一个很好的比较字符串的功能。但是,return ("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);
您可以使用
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 将数字转换为字符串,changeToNum 将数字转换为字符串。
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。
使用Array.sort
、String.fromCharCode
和String.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'
所以我有一组对象。实际上,对象遵循 GeoJSON 规范,因此请记住这一点。在 "properties" 个对象中,存在一个 "name" 的属性。对于每个不同的功能,此名称将是 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 字母到 return 系列中的下一个。在此示例中,'C' 将被视为 MAX,因此我需要 return 'D' 的值。如果我有 AA,那将被认为是 MAX,它会 return 'AB'。如果最大值恰好是 'Z',我想 return 一个 'AA'.
的值
可以忽略小写,只使用26个大写英文字母。没有其他字符。
我相信我可以通过使用 javascript CharCodeAt(index) 以及应用 Math.max、添加 + 1,然后转换回它的 ascii 字符表示来解决这个问题...但我无法将它们整合到一个循环遍历所有这些东西的工作函数中。
不胜感激!
更新: 我使用以下代码部分地使用了它。但是,如果它从 Z 绕到 AA,还没有完全弄清楚如何让它工作。或者如果发现MAX是AF,那么returning AG。 AZ 必须 return BA。
String.fromCharCode(Math.max.apply(Math,someObject.features.map(function(o){return o.properties.name.charCodeAt(0);})) + 1)
其他已知规则:
- 上限可以是 ZZ - 我不太可能需要回到 AAA
- 最大字符不会总是在数组的最后,所以不能 只需获取数组的最后一个特征。
您可以通过以下方式找到 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;
}
}
不过,我仍在努力获取下一个名称。
我想你想排序,其实JS有一个很好的比较字符串的功能。但是,return ("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);
您可以使用
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 将数字转换为字符串,changeToNum 将数字转换为字符串。
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。
使用Array.sort
、String.fromCharCode
和String.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'