user944513 user944513 - 7 months ago 15
Javascript Question

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. I want to add
shortkeys
property to all objects .
Important thing is that I need to give unique
shortkeys
to all object.

Those objects which don't have
shortkeys
will follow these conditions


  • if object don't have
    shortkeys
    than it assign
    shortkeys
    starting from 1-9 like that 'alt+1' ,'alt+2'...'alt+9'.

  • After
    9
    I need to assign
    0
    property to object .

  • Then I need to assign
    a-z
    like that 'alt+a','alt+b'

  • Important I need to skip all
    shortkeys
    which is already defined example 'alt+m' .so that every object has unique property.



Here is my code
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"},

]

Answer

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/

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);