一尘不染

如何将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' }
]

阅读 327

收藏
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