如何将javascript对象转换为键/值数组
作者:互联网
参见英文答案 > How to convert an object to array of objects 5个
给定一个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' }
]
解决方法:
var data = { firstName: 'John', lastName: 'Doe', email: 'john.doe@gmail.com' }
var output = Object.entries(data).map(([key, value]) => ({key,value}));
console.log(output);
受到这个post的启发
标签:javascript,arrays,javascript-objects 来源: https://codeday.me/bug/20190917/1810061.html