请教一下各位大神怎么把下面的原始数据变成目标数据。
就相当于把所有的 key 一层一层拼接起来,组成所需路径,存在当前层级中。(数据层级深度完全随机)
// 原始数据
let fileInfo = [
{
code: [
{
scripts: [
{
server: [
{
bd: [ 'init.ts', 'index.tsx' ],
data: [
'init.ts',
'app.tsx',
{
test: [
{
res: [ 'config.ts' ]
}
]
}
]
}
]
}
]
}
]
}
]
// 目标数据
let fileInfo = [
{
pathName: 'code',
code: [
{
pathName: 'code/scripts',
scripts: [
{
pathName: 'code/scripts/server',
server: [
{
pathName: 'code/scripts/server/bd',
bd: [
{
pathName: 'code/scripts/server/bd/init.ts',
value: 'init.ts'
},
{
pathName: 'code/scripts/server/bd/index.tsx',
value: 'index.tsx'
}
]
},
{
pathName: 'code/scripts/server/data',
data: [
{
pathName: 'code/scripts/server/data/init.ts',
value: 'init.ts'
},
{
pathName: 'code/scripts/server/data/app.tsx',
value: 'app.tsx'
},
{
pathName: 'code/scripts/server/data/test',
test: [
{
pathName: 'code/scripts/server/data/test/res',
res: [
{
pathName: 'code/scripts/server/data/test/res/config.ts',
value: 'config.ts'
}
]
}
]
}
]
}
]
}
]
}
]
}
]
假设你的
init.ts
、index.tsx
、app.tsx
、config.ts
都为字符串,只是你忘记用''
或""
括起来了。js
代码使用
结果