Home > Software design >  Replace element in a scalable way JS
Replace element in a scalable way JS

Time:12-19

I have an Object like this one:

{
  '/': [ 'a', 'd', 10 ],
  a: [ 'e', 20 ],
  e: [ 30 ],
  d: [ 40 ]
}

Is there a effective solution to replace the letters in the arrays with their equivalent value? the result intermediate solution should be like this:

{
  '/': [ 30 , 20 , 40 , 10 ],
  a: [ 30, 20 ],
  e: [ 30 ],
  d: [ 40 ]
}

and final solution the sum of all numbers in the arrays:

{
  '/': [ 100 ],
  a: [ 50 ],
  e: [ 30 ],
  d: [ 40 ]
}

I found a solution, but it's not scalable for a really big Object (hundreds of keys and dozens of letters in the array), so I am looking for suggestions. Thanks.

My solution is a for loop in each element of all arrays, concatenate the equivalent array of the letter element, remove the letter and continue the loop. The loop continues until all arrays are just numbers with a while loop.

CodePudding user response:

Try something like this,

First Sort the keys based on there array element length, so this will help in replcaement logic

let keys = Object.keys(yourObject).sort((x,y) => d[x].length > d[x].length ? 1 : -1)

Then perform iteration on each key

keys.forEach((subArray)=>{
    yourObject[subArray] = yourObject[subArray].map((item)=>{
       
        if(yourObject[item]){
            item = yourObject[item]
        }
        return item
    })
    yourObject[subArray] = yourObject[subArray].flatMap(x=>x)
})

console.log(yourObject)

CodePudding user response:

Again, I am not sure there is a clean solution for this... I may change the process. This is the real Obj

{
  '/': [
    'gqlg',     'hchrwstr',
    'lswlpt',   'plmdrbn',
    'rjwmjd',   'stqq',
    'zjhqnlrr', 'zssdlnc',
    282555
  ],
  gqlg: [
    'hnr',     'mdzdht',
    'gfjqbdp', 'gqlg',
    'nvjqp',   'dwhr',
    'rzsrp',   'vdcbg',
    2458912
  ],
  hchrwstr: [ 'rzsrp', 'shlhgj', 'slwwgc', 340423 ],
  rzsrp: [
    'rzsrp',    'dvzvmsjz',
    'zfn',      'pgwng',
    'fzsngr',   'gqlg',
    'mbsjjzft', 'pmsgd',
    'smd',      1980230
  ],
  dvzvmsjz: [ 149823 ],
  zfn: [ 'cwzbljh', 'fbmww', 'jsst', 1281815 ],
  shlhgj: [
    'wtlngn', 'cjmv',
    'tns',    'lrths',
    'rwn',    'pmldlbbh',
    2303101
  ],
  slwwgc: [ 856006 ],
  lswlpt: [ 278572 ],
  plmdrbn: [ 'fmzjhrq', 227129 ],
  fmzjhrq: [ 'rrjjsvm', 9749 ],
  rrjjsvm: [ 66652 ],
  rjwmjd: [ 'twcrf', 'zwmmrqj', 125294 ],
  twcrf: [ 492080 ],
  zwmmrqj: [ 311312 ],
  stqq: [ 'hpgbcvtb', 'llbthqc', 'rzsrp', 'whmdbn', 863300 ],
  hpgbcvtb: [ 'mqjfvg', 807734 ],
  mqjfvg: [ 170328 ],
  llbthqc: [ 117161 ],
  whmdbn: [ 94821 ],
  zjhqnlrr: [ 'bzzvj', 'ddzmvh', 'dsjd', 'nnzvl', 649511 ],
  bzzvj: [ 'wjtlg', 'zfn', 609500 ],
  wjtlg: [ 97079 ],
  ddzmvh: [ 'cpvfln', 'hgrnmh', 'hjpgf', 'mghmb', 'pvdwhmcr', 1065714 ],
  cpvfln: [ 'sthhc', 274478 ],
  sthhc: [ 'slsj', 'vsvvprg', 719559 ],
  slsj: [ 178013 ],
  vsvvprg: [ 102783 ],
  hgrnmh: [ 'mrnmbbt', 'wpvssts', 'mvrlcvj', 2310065 ],
  mrnmbbt: [ 245579 ],
  hjpgf: [ 749373 ],
  mghmb: [
    'bgrcsdc', 'btjq',
    'gqlg',    'gtwdqw',
    'hgrnmh',  'jts',
    'jvp',     'jvrjcv',
    593716
  ],
  bgrcsdc: [ 235762 ],
  btjq: [
    'bvrcqb',  'drslln',
    'dwzpblb', 'gqlg',
    'hgrnmh',  'hvb',
    'rqtc',    'vsdz',
    'wrnftqh', 0
  ],
  bvrcqb: [ 'gqlg', 82050 ],
  drslln: [ 258102 ],
  dwzpblb: [ 302466 ],
  hnr: [ 'fjd', 'mqp', 681200 ],
  fjd: [ 450522 ],
  mqp: [ 'rzsrp', 0 ],
  pgwng: [ 218271 ],
  mdzdht: [ 'gqlg', 'tvd', 0 ],
  gfjqbdp: [ 143536 ],
  tvd: [ 238491 ],
  hvb: [ 'hgrnmh', 'smmwg', 0 ],
  wpvssts: [ 305634 ],
  smmwg: [ 'rzsrp', 62967 ],
  fzsngr: [ 142687 ],
  rqtc: [ 'pbzhfsg', 'rzsrp', 'shlhgj', 0 ],
  pbzhfsg: [ 41865 ],
  nvjqp: [ 'gcq', 0 ],
  gcq: [ 'mslhm', 'rzsrp', 0 ],
  mslhm: [ 41304 ],
  mbsjjzft: [ 'stgszvmj', 572294 ],
  stgszvmj: [ 'dthcrbmr', 'shlhgj', 'zfn', 206580 ],
  dthcrbmr: [ 'lhp', 'npf', 'qbgrjr', 0 ],
  lhp: [ 37925 ],
  npf: [ 77162 ],
  qbgrjr: [ 48767 ],
  wtlngn: [ 'gqlg', 0 ],
  cjmv: [ 140919 ],
  tns: [ 'qhhtgsvz', 464329 ],
  qhhtgsvz: [ 68979 ],
  vsdz: [ 'jsrqjjtc', 'shlhgj', 347553 ],
  jsrqjjtc: [ 245991 ],
  wrnftqh: [ 252462 ],
  gtwdqw: [ 299426 ],
  jts: [ 542874 ],
  jvp: [ 258220 ],
  jvrjcv: [ 623520 ],
  pvdwhmcr: [
    'bhmmsbmb', 'bjwbvcq',
    'hgrnmh',   'ltvtrtth',
    'prtqcb',   'rmn',
    'shlhgj',   'tmzcmb',
    'vlbqvps',  'wmmfp',
    0
  ],
  bhmmsbmb: [ 'nccjwf', 0 ],
  nccjwf: [ 'drwhjfjn', 206638 ],
  drwhjfjn: [ 291772 ],
  bjwbvcq: [ 'zfn', 0 ],
  ltvtrtth: [ 'fwtwqvn', 'mcgcc', 'zfn', 'zzsnz', 634082 ],
  fwtwqvn: [ 'rqndc', 'rzsrp', 'shlhgj', 124999 ],
  rqndc: [ 45124 ],
  mcgcc: [ 'ngvwhnvf', 249817 ],
  ngvwhnvf: [ 207616 ],
  cwzbljh: [ 134952 ],
  zzsnz: [ 273014 ],
  prtqcb: [ 'dtr', 'rzsrp', 199012 ],
  dtr: [ 'gqlg', 'grwpbs', 'hgrnmh', 647580 ],
  grwpbs: [ 59390 ],
  rmn: [ 'gtnvc', 'rsnqlfsz', 486781 ],
  gtnvc: [ 28523 ],
  rsnqlfsz: [ 'hgrnmh', 'nsfhtthc', 0 ],
  nsfhtthc: [ 284747 ],
  lrths: [ 'hgrnmh', 'pfc', 'vlshvmvj', 'wsn', 703772 ],
  mvrlcvj: [ 121034 ],
  pfc: [ 'gqlg', 0 ],
  vlshvmvj: [ 122494 ],
  wsn: [ 'hgtqj', 'shlhgj', 435231 ],
  hgtqj: [ 214202 ],
  pmldlbbh: [ 263722 ],
  rwn: [ 279589 ],
  tmzcmb: [ 138557 ],
  vlbqvps: [ 'nmfnf', 123659 ],
  nmfnf: [ 'jhpnd', 287610 ],
  jhpnd: [ 290482 ],
  wmmfp: [ 94749 ],
  dsjd: [
    'bwjp',   'gqlg',
    'hgrnmh', 'jntt',
    'vmph',   'zvg',
    865190
  ],
  bwjp: [ 117739 ],
  dwhr: [ 364612 ],
  pmsgd: [ 'wbppspwq', 12648 ],
  wbppspwq: [ 38834 ],
  jntt: [ 286866 ],
  vmph: [ 'fnvpsr', 'gfnvsznj', 224084 ],
  fnvpsr: [ 'flz', 'rzsrp', 552801 ],
  flz: [ 106168 ],
  smd: [ 'drsld', 'gqlg', 419969 ],
  drsld: [ 85885 ],
  gfnvsznj: [ 'fqb', 'hdhmbtj', 'lzdq', 'rlvqmwv', 'rzsrp', 339460 ],
  fqb: [ 465476 ],
  hdhmbtj: [ 'gqlg', 'rzsrp', 'vrmrnt', 135597 ],
  vdcbg: [ 261780 ],
  vrmrnt: [ 418820 ],
  lzdq: [ 425741 ],
  rlvqmwv: [ 'gqlg', 'shlhgj', 398966 ],
  zvg: [ 85701 ],
  nnzvl: [ 'wwzsf', 0 ],
  wwzsf: [ 105177 ],
  zssdlnc: [ 'nfgh', 'zfn', 166004 ],
  nfgh: [ 102281 ],
  fbmww: [ 179734 ],
  jsst: [ 'flp', 0 ],
  flp: [ 99924 ]
}
  • Related