javascript - 将嵌套对象转换为对象的 Json Array
问题描述
我想转换 Json 数组中的嵌套对象。
想转换下面的对象
{
"ErrorPage": {
"PASS": 2
},
"Automated": {
"PASS": 17,
"FAIL": 31
},
"HomePage(Landing page)": {
"PASS": 1,
"FAIL": 6
}
}
进入与下面提到的对象相同的 json 数组
[
{ "category": "ErrorPage"
"PASS": 2
},
{
"category": "Automated"
"PASS": 17,
"FAIL": 31
},
{
"category": "HomePage(Landing page)"
"PASS": 1,
"FAIL": 6
}
]
我正在这样做:
this.httpService.getmoduleTest().subscribe((data) => {
const res = data;
this.Arr = Object.keys(res).map(key=>{
return {
"category": key,
"pass": res[key],
"fail" : res[key]
}
})
console.log(this.Arr);
}
我不知道如何在其中设置通过和失败值。
解决方案
您可以将函数Object.entries
与函数一起使用map
,如下所示:
let obj = {"ErrorPage": {"PASS": 2},"Automated": {"PASS": 17,"FAIL": 31},"HomePage(Landing page)": {"PASS": 1,"FAIL": 6}},
result = Object.entries(obj).map(([category, v]) => ({category, ...v}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
推荐阅读
- javascript - 如何使用jquery获取带有箭头功能的元素的html
- python-3.x - 如何对失败的测试进行单元测试?
- c# - .NET Core WebAPI 应用程序在本地连接,但在服务器上失败并出现 SQL 网络接口错误:26 错误定位服务器/指定实例
- javascript - Golang 和 JavaScript 模块
- typescript - 开玩笑模拟打字稿导入作为依赖项
- python - 建立新连接失败 MAX 重试错误
- lua - 如何记录嵌套表访问/写入?
- ubuntu - 如何修复 CouchDB 复制验证错误?
- python - Pandas 数据框对唯一值进行排序
- vb.net - 在 ShowDialog 期间加载事件期间在应用程序范围内显示沙漏光标