我的对象看起来像这样:
{
"performance": {...},
"a": "",
"b": "N",
"c": true,
"groups": [
{
"acctTypes": [
{
"xxx": "xxx",
"ttt": "ttt",
"accounts": [
{
...
},
{
...
},
{
...
}
],
},
{
"acctTypes": [
{
"xxx": "xxx",
"ttt": "ttt",
"accounts": [
{
...
},
{
...
},
{
...
},
{
...
},
{
...
}
],
},
],
"summary": [
{
...
},
{
...
}
]
}
我正在尝试从中提取所有“帐户”(已查看_js和lodash-但找不到任何相关内容)
结果应该是包含所有帐户的对象/数组
任何帮助将不胜感激。
谢谢
如果obj的结构采用类似的下层结构,则可能对您有用
obj = {
"performance": {},
"a": "",
"b": "N",
"c": true,
"groups": [
{
"acctTypes": [
{
"xxx": "xxx",
"ttt": "ttt",
"accounts": [
{
a:1
},
{
b:2
}
],
},
{
"acctTypes": [
{
"xxx": "xxx",
"ttt": "ttt",
"accounts": [
{
c:3
},
{
d:4
}
],
},
],
"summary": [
{
},
{
}
]
}]}]}
let filtered = obj.groups.reduce(key => key.accounts).acctTypes
let accounts = []
filtered.map(key => {
if (key.accounts) {
accounts.push(...key.accounts)
} else if (key.acctTypes){
accounts.push(...key.acctTypes[0].accounts)
}
})
console.log(accounts)
我已将数据添加到一些PoC帐户中。我测试其中的每个项目是否filtered
包含一个accounts
键,或者acctTypes
其中一个应该包含一个accounts
键,然后accounts
使用ES6扩展将值添加到数组中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句