You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从点分隔字符串动态构建嵌套JavaScript对象?

动态层级的扁平对象转嵌套结构实现方案

问题描述

需要实现一个分组函数,将带点分隔键的扁平对象转换为支持任意深度的嵌套结构。

输入示例(扁平对象)

{
    "0_tags.0": {
        "common": {},
        "native": {
            "topic": "Stromerfassung_251/Zähler0-Leistung"
        }
    },
    "0_tags.0.Various": {
        "common": {},
        "native": {
            "topic": "Stromerfassung_251/Zähler0-Leistung"
        }
    },
    "0_tags.0.Various.Stromerfassung_251/Zähler0-Leistung": {
        "common": {},
        "native": {
            "topic": "Stromerfassung_251/Zähler0-Leistung"
        }
    },
    "0_tags.0.Various.Test-String": {
        "common": {},
        "native": {
            "topic": "Stromerfassung_251/Zähler0-Leistung"
        }
    },
    "0_tags.0.Various.battery_charge": {
        "common": {},
        "native": {
            "topic": "Stromerfassung_251/Zähler0-Leistung"
        }
    }
}

输出示例(嵌套对象)

{
    "0_tags": {
        "0": {
            "Various": {
                "Stromerfassung_251/Zähler0-Leistung": {
                    "common": {},
                    "native": {
                        "topic": "Stromerfassung_252/Zähler0-Leistung"
                    }
                },
                "Test-String": {
                    "common": {},
                    "native": {
                        "topic": "Stromerfassung_253/Zähler0-Leistung"
                    }
                },
                "battery_charge": {
                    "common": {},
                    "native": {
                        "topic": "Stromerfassung_254/Zähler0-Leistung"
                    }
                }
            }
        }
    }
}

现有问题代码

当前实现为硬编码,仅支持固定二级层级,无法适配任意深度的键结构:

for (d in jsonArray) {
    var item = d.split('.');

    if (obj[item[0]] == undefined) {
        obj[item[0]] = {};
    }
    if (obj[item[0]][item[1]] == undefined) {
        obj[item[0]][item[1]] = {};
    }

    obj[item[0]][item[1]][item[2]] = jsonArray[d];
}

解决方案

可以通过迭代逐层构建的方式实现动态层级适配,性能更优且避免递归深度限制:

实现代码

function groupFlatObject(flatObj) {
    const nestedObj = {};
    
    for (const key in flatObj) {
        if (!flatObj.hasOwnProperty(key)) continue;
        
        const keys = key.split('.');
        let current = nestedObj;
        
        // 遍历除最后一个键外的所有层级,按需创建空对象
        for (let i = 0; i < keys.length - 1; i++) {
            const k = keys[i];
            if (!current[k]) {
                current[k] = {};
            }
            current = current[k];
        }
        
        // 将对应值赋值给最底层的键
        const lastKey = keys[keys.length - 1];
        current[lastKey] = flatObj[key];
    }
    
    return nestedObj;
}

代码说明

  • 遍历扁平对象的每个键,按.分割成层级数组
  • 用current变量跟踪当前嵌套层级,逐层检查并创建空对象
  • 最后将对应的值赋值给最底层的键
  • 自动适配任意深度的键结构,无需硬编码层级数

使用示例

const flatData = { /* 你的扁平对象数据 */ };
const nestedData = groupFlatObject(flatData);
console.log(nestedData);

内容的提问来源于stack exchange,提问作者Stef

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 16:06:18