将对象属性转换为对象数组 [英] Converting object properties to array of objects

查看:192
本文介绍了将对象属性转换为对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的输出看起来像这样:

I'm getting output that looks like this:

{'1536135941922': 'true',
 '1536135962942': 'false',
 '1536135986966': 'false',
 '1536135989968': 'true'}

我需要它看起来像这样:

and I need it to look like this:

[{'1536135941922': 'true'},
 {'1536135962942': 'false'},
 {'1536135986966': 'false'},
 {'1536135989968': 'true'}]

所以我的前线可以消耗它.我可以用什么方式转换它?

So my front can consume it. What is the way that I can convert it?

推荐答案

您可以使用Object.entries().map()方法来获得所需的输出:

You can use Object.entries() and .map() methods to get the desired output:

let data = {
  '1536135941922': 'true',
  '1536135962942': 'false',
  '1536135986966': 'false',
  '1536135989968': 'true'
};
  
let result = Object.entries(data).map(( [k, v] ) => ({ [k]: v }));

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }

这篇关于将对象属性转换为对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆