javascript - 如何使用嵌套的子对象迭代嵌套对象键
问题描述
我想迭代嵌套对象键,这些键也将嵌套内部子对象:
嵌套对象代码:
{
"F2C3C496-BEA6-A5E8-15F0-E2867304B463": {
"D39FD497-9529-6CC3-70DE-E8D9277C18D3": {
"child": {
"87A1817D-CFA9-70FD-131B-224658AF13CE": {
"next": {
"F7A90266-B7F4-4CC2-0F1B-D4B320E2EC1A": {}
}
}
},
"next": {
"B49927ED-369E-E219-FC1A-8E4BAAFC3933": {
"next": {}
}
}
}
}
}
JS代码迭代:
flowThrough = (obj, callback, context?, path?: string) => {
let nestedKey=';'
Object.keys(obj).forEach(key => {
if(!isEmptyObject(key))
callback(key, context, path);
if (!isEmpty(obj[key]) && typeof obj[key] === 'object') {
if(obj[key].hasOwnProperty('next'))
nestedKey = obj[key].next;
else if(obj[key].hasOwnProperty('child'))
nestedKey = obj[key].child;
else nestedKey = obj[key];
this.flowThrough(nestedKey, callback, context, (path)? has(context, path.concat(".next"))?path.concat(`.next[${get(context, path.concat(".next").length)}]`): path.concat('.').concat("next[0]") : key)
}
})
}
实际上,上面的代码适用于一个嵌套级别来获取 key(ids)。如果它到达空对象键,则循环将在那里结束。实际上,它应该检查任何其他孩子/下一个是否存在。
预期输出:
{ "flow": [
{
"id": "F2C3C496-BEA6-A5E8-15F0-E2867304B463",
"next": [
{
"id": "D39FD497-9529-6CC3-70DE-E8D9277C18D3",
"child": [
{
"id": "87A1817D-CFA9-70FD-131B-224658AF13CE",
"next": [
{
"id": "F7A90266-B7F4-4CC2-0F1B-D4B320E2EC1A",
"next": []
}
]
}
],
"next": [
{
"id": "B49927ED-369E-E219-FC1A-8E4BAAFC3933",
"next": []
}
]
}
]
}
]
}
请给我解决方案。
解决方案
let obj = {
"F2C3C496-BEA6-A5E8-15F0-E2867304B463": {
"D39FD497-9529-6CC3-70DE-E8D9277C18D3": {
"child": {
"87A1817D-CFA9-70FD-131B-224658AF13CE": {
"next": {
"F7A90266-B7F4-4CC2-0F1B-D4B320E2EC1A": {}
}
}
},
"next": {
"B49927ED-369E-E219-FC1A-8E4BAAFC3933": {
"next": {}
}
}
}
}
}
const setObj = e => {
let tmp = {}
if (Array.isArray(e[0])) e = e[0]
if (typeof e[0] === 'string' && e[0].split('-').length === 5) {
tmp = {
id: e[0],
next: setObj(Object.entries(e[1]))
};
} else if (e[1]) {
tmp = {
child: setObj(Object.entries(e[1]))
};
}
return tmp
}
let newobj = Object.entries(obj).map(setObj)[0]
console.log(newobj)
原始答案...这是解决问题的一种方法。使用递归函数和Object.entries来收集数据。我发现结果是一系列嵌套的 id 数组 - 所以我将它们全部展平join()
,然后再次拆分它们。最后,filter()
帮助删除了空数组索引
let obj = {
"F2C3C496-BEA6-A5E8-15F0-E2867304B463": {
"D39FD497-9529-6CC3-70DE-E8D9277C18D3": {
"child": {
"87A1817D-CFA9-70FD-131B-224658AF13CE": {
"next": {
"F7A90266-B7F4-4CC2-0F1B-D4B320E2EC1A": {}
}
}
},
"next": {
"B49927ED-369E-E219-FC1A-8E4BAAFC3933": {
"next": {}
}
}
}
}
}
const getKeys = e => {
let ret = []
if (e[0].split('-').length === 5) ret.push(e[0]);
if (e[1]) ret.push(Object.entries(e[1]).map(getKeys))
return ret.join(',')
}
let keys = Object.entries(obj).map(getKeys)[0].split(",").filter(e => e)
console.log(keys)
推荐阅读
- python - 如何在 tkinter 下拉列表中放置函数?
- python - 使用 python 的 plotly cloropleth 绘制 GeoJSON 问题
- applescript - Applescript使用预览将文件夹中的所有文件导出为pdf
- algorithm - 是否有一种快速的概率算法来计算用户之间的相互事物的数量?
- nest-device-access - 为 master 项目使用嵌套设备访问控制台
- c# - Azure 函数对象返回未返回正确的嵌套值
- c# - 根据提供的路径重定向到操作不起作用
- javascript - HTML 画布线条颜色不准确
- python - 如何从数据框列表中创建单独的数据框?例如 df1 = df[0]
- powerbi - Power BI Report Builder 在设计报表后尝试运行时冻结