小编典典

如何将javascript对象转置为键/值数组

javascript

给定一个JavaScript对象,如何将其转换为对象数组(每个对象都带有键,值)?

例:

var data = { firstName: 'John', lastName: 'Doe', email: 'john.doe@gmail.com' }

结果像:

[
  { key: 'firstName', value: 'John' },
  { key: 'lastName', value: 'Doe' },
  { key: 'email', value: 'john.doe@gmail.com' }
]

阅读 365

收藏
2020-05-01

共1个答案

小编典典

var data = { firstName: ‘John’, lastName: ‘Doe’, email: 'john.doe@gmail.com‘ }

var output = Object.entries(data).map(([key, value]) => ({key,value}));



console.log(output);
2020-05-01