export function setNestedValue(obj: UTSJSONObject, path: string, value: any): T {
// 将路径字符串分割为数组
const keys = path.split(".");
let current = obj;
// 遍历路径中的每个键,除了最后一个键
for (let i = 0; i < keys.length - 1; i++) {
const key = keys[i];
// 如果当前层级不存在,则创建一个新对象
if (!current[key]) {
current[key] = {};
}
// 进入下一层级
current = current[key];
}
// 对最后一个键进行赋值
const lastKey = keys[keys.length - 1];
current[lastKey] = value;
return obj; // 返回更新后的对象
}
0 个回复