javascript - 点符号对象到多维对象
问题描述
具有以下数据格式:
const flat = {
'100': 'L1',
'100.200': 'L1, J2',
'100.200.300': 'L1, J2, A3',
'100.200.400': 'L1, J2, A4',
'100.300': 'L1, J3',
'100.400.500': 'L1, J4, A5'
};
我想把它改成这样:
{
"100":{
"name":"L1",
"children":{
"200":{
"name":"L1, J2",
"children":{
"300":{
"name":"L1, J2, A3"
},
"400":{
"name":"L1, J2, A4"
}
}
},
"300":{
"name":"L1, J3"
},
"400":{
"name":null,
"children":{
"500":{
"name":"L1, J4, A5"
}
}
}
}
}
}
我一直在尝试从这篇 Stack Overflow 帖子中实现一些东西,尽管它们只涵盖了我的很多用例,而且很难弄清楚。
解决方案
这是一个使用对提供的对象的Array.prototype.reduce()
调用的示例。Object.entries()
function expand(obj) {
return Object
.entries(obj)
.reduce((a, [propString, name]) => {
const
propArr = propString.split('.'),
innerProp = propArr.pop(),
innerObj = propArr.reduce((_a, prop) => (
_a[prop] ??= {}, _a[prop]), a);
innerObj[innerProp] = { name };
return a;
}, {})
}
const flat = { '100': 'L1', '100.200': 'L1, J2', '100.200.300': 'L1, J2, A3', '100.200.400': 'L1, J2, A4', '100.300': 'L1, J3', '100.400.500': 'L1, J4, A5' };
console.log(expand(flat));
.as-console-wrapper { max-height: 100% !important; top: 0; }
但是您链接的问题Convert javascript dot notation object to nested object,如果您进行一次更改以适应输入的形状,就可以正常工作。
target[parts[0]] = obj[objectPath]
变成
target[parts[0]] = { name: obj[objectPath] }
// https://stackoverflow.com/questions/7793811/convert-javascript-dot-notation-object-to-nested-object
function deepen(obj) {
const result = {};
// For each object path (property key) in the object
for (const objectPath in obj) {
// Split path into component parts
const parts = objectPath.split('.');
// Create sub-objects along path as needed
let target = result;
while (parts.length > 1) {
const part = parts.shift();
target = target[part] = target[part] || {};
}
// Set value at end of path
target[parts[0]] = { name: obj[objectPath] }
}
return result;
}
const flat = { '100': 'L1', '100.200': 'L1, J2', '100.200.300': 'L1, J2, A3', '100.200.400': 'L1, J2, A4', '100.300': 'L1, J3', '100.400.500': 'L1, J4, A5' };
console.log(deepen(flat));
.as-console-wrapper { max-height: 100% !important; top: 0; }
推荐阅读
- postgresql - 在 Springboot 应用程序上同步 Elasticsearch Postgresql
- java - 在 Java8 中使用流重写
- c - perf_event 报告意外的缓存未命中量:访问数组中的 N 个元素会导致 2N 次缓存未命中
- python - 尝试从列表中删除重复时位于位置打印语句的 Python 生成器对象
- r - 用数字替换文本值
- gcloud - GCloud 构建 YAML 替换不起作用
- ios - 如何在我们应用快照时停止 UICollectionViewDiffableDataSource 滚动集合视图?
- reactjs - 我有样式组件,想迁移到 scss,
- java - 特殊字符未写入 csv 文件
- python-3.x - 正则表达式 - 如果外括号前面有特定字符,则不匹配任何内括号