跪求用js/ts方法解决以下数据格式转换?

请教一下各位大神怎么把下面的原始数据变成目标数据。
就相当于把所有的 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'
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]
阅读 1.7k
2 个回答

假设你的 init.tsindex.tsxapp.tsxconfig.ts 都为字符串,只是你忘记用 ''"" 括起来了。

js 代码

let f = (a, p = []) => a.flatMap(i => typeof(i) === 'string' ? {
        pathName: [...p, i].join('/'),
        value: i,
    } : Object.entries(i).map(([k, v]) => ({
        pathName: [...p, k].join('/'),
        [k]: f(v, [...p, k]),
    }))
);

使用

f(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"
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]
function setPath(arr) {
      function joinPath(prev, next) {
        return prev ? `${prev}/${next}` : `${next}`;
      }

      function fn(item, str) {
        if (typeof item === 'string') {
          return {
            pathName: joinPath(str, item),
            value: item
          }
        } else if (Array.isArray(item)) {
          return item.map(o => fn(o, str)).flat(2);
        } else {
          let keys = Object.keys(item);
          let obj = {};
          if (keys.length === 0) {
            return item;
          }
          if (keys.length > 1) {
            return keys.map(key => {
              let temp = {};
              temp[key] = fn(item[key], joinPath(str, key));
              temp.pathName = joinPath(str, key);
              return temp;
            });
          }
          const key = keys[0];
          obj[key] = fn(item[key], joinPath(str, key));
          obj.pathName = joinPath(str, key);
          return obj;
        }
      }
      let a = [];
      for (let i = 0; i < arr.length; i++) {
        const value = fn(arr[i], '');
        a.push(value);
      }
      return a;

    }
推荐问题
logo
Microsoft
子站问答
访问
宣传栏