-2

My goal here is to convert all type values in the array below to the types in the object that collerate to numbers.

let obj = [
  {
    type: 'boolean',
    name: 'coolName',
    description: 'First description',
    required: false
  },
  {
    type: 'subcommand',
    name: 'destroy',
    description: 'Destroy something',
    options: [
      {
        type:"integer",
        name:"amount",
        description:"How much would you like to destroy?",
        required: true
      }
    ]
  }
]

 const types = {
    'subcommand':1,
    'subcommandgroup':2,
    'string':3,
    'integer':4,
    'boolean':5,
    'user':6,
    'channel':7,
    'role':8,
    'mentionable':9,
    'number':10,
    'attachment':11
}

I've been looking for a while and cannot find a function that also iterates through the nested object, if anyone has a way to do this please let me know.

CheryX
  • 9
  • 2
  • 3
  • Can you add what is you result attempt? – Alaindeseine Aug 10 '22 at 19:37
  • It looks like you are wanting to [traverse all the nodes of a json object tree with javascript](https://stackoverflow.com/questions/722668/traverse-all-the-nodes-of-a-json-object-tree-with-javascript). The difference would be writing a process function to swap your values accordingly, but this should get you started and/or point you in the right direction. – EssXTee Aug 10 '22 at 19:46
  • Can the nesting be more than 1 level deep? Then you'll need to write a recursive function. – Barmar Aug 10 '22 at 19:48

1 Answers1

0
obj.map(o => {
    o.type = types[o.type]
  if(o.options){
    o.options.map(opt => {
        opt.type = types[opt.type]
    })
  } 
  return o;
})
Simran Birla
  • 136
  • 6