简体   繁体   English

如何在javascript中按递增顺序在对象中添加属性?

[英]how to add property in object in increasing order in javascript?

I have array of objects .In Objects some have shortkeys property and some don't have shortkeys property.我有对象数组。在对象中,有些具有shortkeys属性,有些没有shortkeys属性。 I want to add shortkeys property to all objects .我想为所有对象添加shortkeys属性。 Important thing is that I need to give unique shortkeys to all object.重要的是我需要为所有对象提供唯一的shortkeys

Those objects which don't have shortkeys will follow these conditions那些没有shortkeys对象将遵循这些条件

  • if object don't have shortkeys than it assign shortkeys starting from 1-9 like that 'alt+1' ,'alt+2'...'alt+9'.如果对象没有shortkeys ,则它会分配从1-9开始的shortkeys ,例如 'alt+1' 、'alt+2'...'alt+9'。
  • After 9 I need to assign 0 property to object . 9之后,我需要将0属性分配给 object 。
  • Then I need to assign az like that 'alt+a','alt+b'然后我需要像'alt+a','alt+b'一样分配az
  • Important I need to skip all shortkeys which is already defined example 'alt+m' .so that every object has unique property.重要的是,我需要跳过已经定义的所有shortkeys ,例如“alt+m”。这样每个对象都具有唯一的属性。

Here is my code https://jsfiddle.net/krzz9zmf/这是我的代码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';
    }
  }
}

Expected Output预期产出

[
{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"},

]

You can go through and remove the chars that exists as keys then go back through and add what remains您可以遍历并删除作为键存在的字符,然后返回并添加剩余的字符

https://jsfiddle.net/stevenkaspar/krzz9zmf/8/ 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);

 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>

The errors with your existing attempt (aside from not getting the letters you want) was that...您现有尝试的错误(除了没有得到您想要的字母)是...

  • you were using the wrong variable name myObj after having declared var obj您在声明var obj后使用了错误的变量名myObj
  • you had if (!'shortkeys' in myObj){ instead of if (!('shortkeys' in myObj)){你有if (!'shortkeys' in myObj){而不是if (!('shortkeys' in myObj)){
  • you were failing to look for existing shortkeys before assigning one在分配一个shortkeys之前,你没有找到现有的shortkeys

In this code, I...在这段代码中,我...

  • added an object that tracks all used shortkeys, including the currently existing ones添加了一个跟踪所有使用过的快捷键的对象,包括当前存在的快捷键
  • added a loop that keeps looking for a unique shortkey until one is found添加了一个循环,不断寻找唯一的快捷键,直到找到为止
  • added a alpha variable to be used to get the az character添加了一个用于获取az字符的alpha变量
  • added an OUTER label to the main for loop so that we can break the loop if we exceed the letter z .在主for循环中添加了一个OUTER标签,以便我们可以在超出字母z中断循环。 The label is needed because we're breaking from the inner do-while loop.标签是必需的,因为我们正在打破内部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 'az' 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 { 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>

Note...笔记...

  • Your expected output has a mistake;您的预期输出有误; it has alt+e twice.它有两次alt+e
  • There's no need to manually create an Array of values since characters map to numbers由于字符映射到数字,因此无需手动创建值数组

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

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