繁体   English   中英

用特定键替换 object 中的所有值

[英]Replace all values in an object with a specific key

我的目标是将下面数组中的所有类型值转换为 object 中与数字相关的类型。

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
}

我一直在寻找一段时间,但找不到同样遍历嵌套 object 的 function,如果有人有办法做到这一点,请告诉我。

obj.map(o => {
    o.type = types[o.type]
  if(o.options){
    o.options.map(opt => {
        opt.type = types[opt.type]
    })
  } 
  return o;
})

暂无
暂无

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

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