简体   繁体   English

我想按字母顺序生成唯一的字母键| angularJs

[英]I want to generate unique alphabetical keys in alphabetical order |angularJs

I want to generate keys in alphabetical order in such a way that it begins with 我想以字母顺序生成键,使其以

aaaaa AAAAA

then the next would be aaaab and after reaching to aaaaz the string should be aaaba and then aaabb and so on , so that the keys generate properly 那么下一个将是aaaab ,到达aaaaz ,字符串应该是aaaba ,然后是aaabb ,依此类推,以便密钥可以正确生成

My sample JSON to be created 我要创建的示例JSON

 var keygen={aaaaa,
             aaaab,
             aaaac .........aaaaz,aaaba ....}

my javascript 我的JavaScript

$scope.doKeyGen=function(lastValueInJSON)
                {  // Do something 
                   }

This will work for you. 这将为您工作。 JS Fiddle is here. JS Fiddle在这里。

https://jsfiddle.net/3d789okv/7/ https://jsfiddle.net/3d789okv/7/

Make sure that you the last value you give will be hit. 确保您输入的最后一个值将被打中。 Otherwise you are going to an infinite loop hell. 否则,您将陷入无限循环的地狱。 Also you can configure the number of letters in the call to getNext(). 您还可以配置对getNext()的调用中的字母数。 But make sure that you set the equal number of letters in the first value and "aaaaa" and the last value "asxas" 但是请确保在第一个值和“ aaaaa”和最后一个值“ asxas”中设置相等数量的字母

 String.prototype.replaceAt=function(index, replacement) {
        return this.substr(0, index) + replacement+ this.substr(index + replacement.length);
    }

 var json = [];
 function getNext(charCount,lastValue){
    changeIndex = charCount -1;
   var newValue = "";
     while (changeIndex >= 0){
        if(lastValue[changeIndex] !== "z"){
           var changed = lastValue[changeIndex];
           var replacechanged = String.fromCharCode(changed.charCodeAt(0)+1);
           newValue = lastValue.replaceAt(changeIndex,replacechanged)
           for(var j=changeIndex+1; j < charCount; ++j){
              newValue = newValue.replaceAt(j,"a");
           }
           return newValue;
         }
         changeIndex--;
      }
 }

 function createJSON(lastValue){
    if(!json.length){
      //var startPrefix = "aaaaa";
      json.push("aaaaa");
      while(lastValue !== json[json.length-1]){

         json.push(getNext(5,json[json.length-1]));
      }
      console.log(json);
    }
 }

 createJSON("aaabz");

You need to use recursive function to generate your keys. 您需要使用递归函数来生成密钥。 I've written some piece of code in this fiddle link , which generate keys as per your requirement and create JSON too. 我已经在该小提琴链接中编写了一些代码,该代码根据您的要求生成密钥并创建JSON。

Please note I assume small case alphabets keys only. 请注意,我仅假设使用小写字母键。 and used 3 length string (aaa), You can use 4 length also but performance degrades. 并使用3个长度的字符串(aaa),您也可以使用4个长度的字符串,但性能会下降。

You can change any first key in input in attached fiddle, like 'aay' then code generate next all possible keys.(aaz, aba,.....,zzz). 您可以在附加的小提琴中更改输入中的任何第一个键,例如'aay'然后代码会生成下一个所有可能的键。(aaz,aba,.....,zzz)。

You can use this 你可以用这个

 function getNextKey(lastKeyCode, changeIndex) { var charCodes = []; if( changeIndex == undefined ) changeIndex = lastKeyCode.length - 1; if(changeIndex - 1 > -1 && lastKeyCode.charCodeAt(changeIndex) == 122 ) { lastKeyCode = getNextKey(lastKeyCode, changeIndex - 1); } lastKeyCode.split('').forEach(function(e){charCodes.push(e.charCodeAt())}); charCodes[changeIndex] = 97 + (charCodes[changeIndex] - 96 ) % 26; return String.fromCharCode.apply(0, charCodes); } //-------------------EDIT ( GENERATE KEYS LIKE THIS )------------ function generateKeys(lastKey) { var json = []; var nextKey = new Array(lastKey.length + 1 ).join('a'); json.push(nextKey); while( nextKey != lastKey ) { json.push( (nextKey = getNextKey(nextKey)) ) } return json; } //---------------------------Example---------------------------- var last = 'test'; console.log('Last Key : '+last+' | Generated key length : '+generateKeys(last).length); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM