2016-05-06 51 views
-4

我有數組對象。在對象中有的有shortkeys屬性,有的沒有shortkeys屬性。我想將shortkeys屬性添加到所有對象。 重要的是我需要給所有對象賦予唯一的shortkeys如何在javascript中按遞增順序添加對象中的屬性?

不具有shortkeys將遵循這些條件下這些對象

  • 如果對象沒有shortkeys比它分配shortkeys1-9開始這樣的「ALT + 1」,'ALT +2' ... 'ALT + 9'。
  • 9我需要將0屬性分配給對象。
  • 然後我需要分配a-z像「ALT + A」,「ALT + B」
  • 重要我需要跳過其中已定義示例的「ALT + M」所有shortkeys是.so每個對象具有獨特的屬性。

這裏是我的代碼 https://jsfiddle.net/krzz9zmf/

var arr=[ 
    {name:"abc",shortkeys:"alt+m"}, 
    {name:"c_1"}, 
    {name:"abc",shortkeys:"alt+t"}, 
    {name:"abc",shortkeys:"alt+c"}, 
    {name:"wes_2"}, 
    {name:"ncv_3"}, 
    {name:"sghb_4"}, 
    {name:"ijo_5"}, 
    {name:"nhio_6"}, 
    {name:"jion_7"}, 
    {name:"chudoi_8"}, 
    {name:"bdmki_9"}, 
    {name:"dssd_0"}, 
    {name:"sdfs_a"}, 
    {name:"abc",shortkeys:"alt+y"}, 
    {name:"abc",shortkeys:"alt+e"}, 
    {name:"sghb_b"}, 
    {name:"ijo_d"}, 
    {name:"gsha_e"}, 
    {name:"asdas_f"}, 
    {name:"bbb_g"}, 
    {name:"mko_h"}, 
    {name:"kioh_i"}, 
    {name:"qwee_j"}, 
    {name:"qwee_k"}, 
    {name:"qwee_l"}, 
    {name:"qwee_n"}, 

] 

var j =1; 
for(var i=0;i<arr.length;i++){ 
    var obj =arr[i]; 
    if (!'shortkeys' in myObj){ 
    //add shorkeys start from 1-9 then 0 and then a-z.In other words need to assign shortcut key like that 'alt+1','alt+2'....'alt+0'''alt+a','alt+b'...'alt+z'; 
    //some of the objects already define shortkeys example 'alt+m' .so I need to skip these shortcut key .so that each item have unique shortcut. 
    myObj.shortkeys= 'alt+'+j; 
    j++; 
    if(j==10){ 
     j=0 
    } 
    if(j==1){ 
     myObj.shortkeys='alt+a'; 
    } 
    } 
} 

期望輸出

[ 
{name:"abc",shortkeys:"alt+m"}, 
{name:"c_1",,shortkeys:"alt+1"}, 
{name:"abc",shortkeys:"alt+t"}, 
{name:"abc",shortkeys:"alt+c"}, 
{name:"wes_2",shortkeys:"alt+2"}, 
{name:"ncv_3",,shortkeys:"alt+3"}, 
{name:"sghb_4",shortkeys:"alt+4"}, 
{name:"ijo_5",shortkeys:"alt+5"}, 
{name:"nhio_6",shortkeys:"alt+6"}, 
{name:"jion_7",shortkeys:"alt+7"}, 
{name:"chudoi_8",shortkeys:"alt+8"}, 
{name:"bdmki_9",shortkeys:"alt+9"}, 
{name:"dssd_0",shortkeys:"alt+0"}, 
{name:"sdfs_a",shortkeys:"alt+a"}, 
{name:"abc",shortkeys:"alt+y"}, 
{name:"abc",shortkeys:"alt+e"}, 
{name:"sghb_b",shortkeys:"alt+b"}, 
{name:"ijo_d",shortkeys:"alt+d"}, 
{name:"gsha_e",shortkeys:"alt+e"}, 
{name:"asdas_f",shortkeys:"alt+f"}, 
{name:"bbb_g",shortkeys:"alt+g"}, 
{name:"mko_h",shortkeys:"alt+h"}, 
{name:"kioh_i",shortkeys:"alt+i"}, 
{name:"qwee_j",shortkeys:"alt+j"}, 
{name:"qwee_k",shortkeys:"alt+k"}, 
{name:"qwee_l",shortkeys:"alt+l"}, 
{name:"qwee_n",shortkeys:"alt+n"}, 

] 
+0

只是想知道,是什麼ALT + z'後'? –

+2

我不確定你是否真的爲你所做的工作付出了一些努力(我希望你做到了),但是有太多的錯誤和缺失的部分,我不能假裝花太多時間在它上面。這是一個工作解決方案(來自你的小提琴):https://jsfiddle.net/krzz9zmf/2/。請注意,你既沒有正確檢查,也沒有替換值,甚至沒有引用正確的變量(檢查你的控制檯,它會告訴你是否出現錯誤**)。你正在引用myObj而不是obj。 (我甚至沒有檢查所有結果是否正確,因爲邏輯丟失,這是我能做的最好的) – briosheje

+0

你沒有說你的問題實際上是什麼,但你的代碼有'var obj',但是你改用'myObj'。 – 2016-05-06 13:21:51

回答

1

你可以通過和刪除存在的鑰匙,然後回去通過並添加剩下的字符

https://jsfiddle.net/stevenkaspar/krzz9zmf/8/

var alpha_num_array = [ 
'1','2','3','4', 
'5','6','7','8','9', 
'0', 
'a','b','c','d','e', 
'f','g','h','i','j', 
'k','l','m','n','o', 
'p','q','r','s','t', 
'u','v','w','x','y', 
'z']; 

key_arr.map(function(k){ 
    if(!k.shortkeys) return; 

    var key = k.shortkeys.split('+')[1]; 
    var key_index = alpha_num_array.indexOf(key); 
    alpha_num_array.splice(key_index, 1); 

}) 

key_arr = key_arr.map(function(k){ 
    if(k.shortkeys) return k; 

    k.shortkeys = 'alt+'+alpha_num_array[0]; 
    alpha_num_array.shift(); 
    return k; 
}) 

console.log(key_arr); 
+0

..請移除此答案 – user944513

+0

再試一次。我重新排列陣列,所以它將是1-9,0,az命令 –

+0

請分享jsfiddle – user944513

1

var arr=[ 
 
    {name:"abc",shortkeys:"alt+m"}, 
 
    {name:"c_1"}, 
 
    {name:"abc",shortkeys:"alt+t"}, 
 
    {name:"abc",shortkeys:"alt+c"}, 
 
    {name:"wes_2"}, 
 
    {name:"ncv_3"}, 
 
    {name:"sghb_4"}, 
 
    {name:"ijo_5"}, 
 
    {name:"nhio_6"}, 
 
    {name:"jion_7"}, 
 
    {name:"chudoi_8"}, 
 
    {name:"bdmki_9"}, 
 
    {name:"dssd_0"}, 
 
    {name:"sdfs_a"}, 
 
    {name:"abc",shortkeys:"alt+4"}, 
 
    {name:"abc",shortkeys:"alt+e"}, 
 
    {name:"sghb_b"}, 
 
    {name:"ijo_d"}, 
 
    {name:"gsha_e"}, 
 
    {name:"asdas_f"}, 
 
    {name:"bbb_g"}, 
 
    {name:"mko_h"}, 
 
    {name:"kioh_i"}, 
 
    {name:"qwee_j"}, 
 
    {name:"qwee_k"}, 
 
    {name:"qwee_l"}, 
 
    {name:"qwee_n"}, 
 
]; 
 

 
//possible shortkeys reversed so we can pop them off 
 
var keys = [ 
 
    '1','2','3','4','5','6','7','8','9','0' 
 
    ,'a','b','c','d','e','f','g','h','i','j' 
 
    ,'k','l','m','n','o','p','q','r','s','t' 
 
    ,'u','v','w','x','y','z'].reverse(); 
 
//elements without a shortkeys 
 
var tagsWithoutShortcuts = arr.filter(function(element){ return typeof element.shortkeys === 'undefined'; }); 
 

 
console.log(keys); 
 
console.log(tagsWithoutShortcuts); 
 

 
tagsWithoutShortcuts.forEach(function(tag){ 
 
    var key = keys.pop(); 
 
    //while key is already used, get another 
 
    while (arr.filter(function(element){ return element.shortkeys === 'alt+'+ key; }).length) key = keys.pop(); 
 
    //put the shortkeys on the tag 
 
    tag.shortkeys = 'alt+'+ key; 
 
}); 
 

 
console.log(arr);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

+0

您的輸出是錯誤的。'alt + 4'缺失 – user944513

+0

你的輸出不期望output.so請刪除這個答案 – user944513

+0

我把alt + 4放在arr []的開頭。再檢查一遍。 '{name:「abc」,shortkeys:「alt + 4」},'否則你的測試開始數據沒有帶alt +#的元素來測試解決方案會跳過一個數字。 – Taplar

1

與您現有的嘗試(除了沒有得到你想要的字母)的錯誤是......

  • 您正在使用宣佈後的錯誤變量名稱myObjvar obj
  • 你有if (!'shortkeys' in myObj){而不是if (!('shortkeys' in myObj)){
  • 你沒有找對現有shortkeys分配一個

在此代碼之前,我...

  • 添加了一個對象,跟蹤所有使用的快捷鍵,其中包括目前已有的
  • 添加一個循環,直到找到一個
  • 增加了alpha變量被用來獲得該不斷尋找一個獨特的shortkey a-z字符
  • OUTER標籤添加到主for循環,以便我們可以打破循環,如果我們超過字母z。標籤是需要的,因爲我們正在從內部do-while循環中分離出來。

var arr=[ 
 
    {name:"abc",shortkeys:"alt+m"}, {name:"c_1"}, {name:"abc",shortkeys:"alt+t"}, {name:"abc",shortkeys:"alt+c"}, 
 
    {name:"wes_2"}, {name:"ncv_3"}, {name:"sghb_4"}, {name:"ijo_5"}, {name:"nhio_6"}, {name:"jion_7"}, 
 
    {name:"chudoi_8"}, {name:"bdmki_9"}, {name:"dssd_0"}, {name:"sdfs_a"}, {name:"abc",shortkeys:"alt+y"}, 
 
    {name:"abc",shortkeys:"alt+e"}, {name:"sghb_b"}, {name:"ijo_d"}, {name:"gsha_e"}, {name:"asdas_f"}, 
 
    {name:"bbb_g"}, {name:"mko_h"}, {name:"kioh_i"}, {name:"qwee_j"}, {name:"qwee_k"}, {name:"qwee_l"}, {name:"qwee_n"} 
 
] 
 

 
var j = 1; 
 
var alpha = 'a'.charCodeAt(0); // Number for the 'a-z' chars 
 
var z = 'z'.charCodeAt(0); // Number for the 'z' character 
 
var shorts = {}; // Holds all shortkeys to avoid duplicates 
 
var short = ""; 
 

 
// First gather all the existing shortkeys 
 
for (var i = 0; i < arr.length; i++) { 
 
    if (arr[i].shortkeys) { 
 
    shorts[arr[i].shortkeys] = arr[i].shortkeys 
 
    } 
 
} 
 

 
OUTER: 
 
for (var i = 0; i < arr.length; i++) { 
 
    var myObj = arr[i]; 
 
    
 
    if (!('shortkeys' in myObj)) { 
 
    // We want to make sure that our "short" is not yet used. 
 
    do { 
 
     if (j < 10) { 
 
     short = 'alt+' + j 
 
     j++; 
 
     
 
     } else if (j == 10) { 
 
     short = 'alt+0' 
 
     j++; 
 
     
 
     } else if (alpha <= z) { 
 
     short = 'alt+' + String.fromCharCode(alpha); 
 
     alpha++; 
 
     
 
     } else { 
 
     \t break OUTER; // We've moved beyond 'z', so just quit. 
 
     } 
 
    } while(short in shorts) 
 
    
 
    // Track the new shortkey, and assign it 
 
    shorts[short] = short 
 
    myObj.shortkeys = short 
 
    } 
 
} 
 

 
document.querySelector("pre").textContent = JSON.stringify(arr, null, 4)
<pre></pre>

注...

  • 你的預期輸出有錯誤;它有alt+e兩次。
  • 沒有必要手動創建以來字符值的數組映射到數字
相關問題