【问题标题】:Converting objects to array in Javascript [duplicate]在Javascript中将对象转换为数组[重复]
【发布时间】:2021-04-27 21:45:30
【问题描述】:

我想转换一下:

const words = {told: 64,
               mistake: 11,
               thought: 16,
               bad: 17}

const words = [ 
  {text: 'told', value: 64,},
  {text: 'mistake', value: 11,},
  {text: 'thought', value: 16,},
  {text: 'bad', value: 17,
  },
]

非常感谢

【问题讨论】:

  • 对象条目和映射

标签: javascript reactjs


【解决方案1】:

您可以使用Object.entries() 循环遍历对象的(键和值):

const data = {told: 64,
               mistake: 11,
               thought: 16,
               bad: 17
             };
const words = Object.entries(data).map(([text, value]) => ({text, value}));
console.log(words);

【讨论】:

    猜你喜欢
    • 2020-02-06
    • 2018-03-18
    • 2015-04-21
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 2021-01-04
    • 2021-05-09
    • 1970-01-01
    相关资源
    最近更新 更多