javascript对象多维以一维数组

我有这样的变量rawData:

let rawData = [
  {
    title: '1',
    result: '1',
    child: [
      {
        title: '1-1',
        result: '1-1',
        child: [
          {
            title: '1-1-1',
            result: '1-1-1',
            child: [
              {
                title: '1-1-1-1',
                result: '1-1-1-1',
                child: [
                  {
                    title: '1-1-1-1-1',
                    result: '1-1-1-1-1',
                  },
                ],
              },
              {
                title: '1-1-1-2',
                result: '1-1-1-2',
              },
              {
                title: '1-1-1-3',
                result: '1-1-1-3',
              },
            ],
          },
        ],
      },
    ],
  },
];

和我的功能。它按预期运行,这是我的功能:

let normalizeArray = [];
function test(array) {
  for (const key in array) {
    if (array.hasOwnProperty(key)) {
      const element = array[key];
      if (element.hasOwnProperty('child')) {
        test(element.child);
        delete element.child;
        normalizeArray.unshift(Object.assign({}, element));
      } else {
        normalizeArray.push(Object.assign({}, element));
      }
    }
  }
}

然后返回(预期):

[
  { title: '1', result: '1' },
  { title: '1-1', result: '1-1' },
  { title: '1-1-1', result: '1-1-1' },
  { title: '1-1-1-1', result: '1-1-1-1' },
  { title: '1-1-1-1-1', result: '1-1-1-1-1' },
  { title: '1-1-1-2', result: '1-1-1-2' },
  { title: '1-1-1-3', result: '1-1-1-3' },
];

但是,如果rawData像这样:

let rawData = [
  {
    title: '1',
    result: '1',
    child: [
      {
        title: '1-1',
        result: '1-1',
        child: [
          {
            title: '1-1-1',
            result: '1-1-1',
            child: [
              {
                title: '1-1-1-1',
                result: '1-1-1-1',
                child: [
                  {
                    title: '1-1-1-1-1',
                    result: '1-1-1-1-1',
                  },
                ],
              },
              {
                title: '1-1-1-2',
                result: '1-1-1-2',
              },
              {
                title: '1-1-1-3',
                result: '1-1-1-3',
              },
            ],
          },
        ],
      },
    ],
  },
  {
    title: '2',
    result: '2',
    child: [
      {
        title: '2-2',
        result: '2-2',
      },
    ],
  },
  {
    title: '3',
    result: '3',
  },
];

与我的功能,它返回:

[
  { title: '2', result: '2' },
  { title: '1', result: '1' },
  { title: '1-1', result: '1-1' },
  { title: '1-1-1', result: '1-1-1' },
  { title: '1-1-1-1', result: '1-1-1-1' },
  { title: '1-1-1-1-1', result: '1-1-1-1-1' },
  { title: '1-1-1-2', result: '1-1-1-2' },
  { title: '1-1-1-3', result: '1-1-1-3' },
  { title: '2-2', result: '2-2' },
  { title: '3', result: '3' },
];

我如何得到这样的结果:

[
  { title: '1', result: '1' },
  { title: '1-1', result: '1-1' },
  { title: '1-1-1', result: '1-1-1' },
  { title: '1-1-1-1', result: '1-1-1-1' },
  { title: '1-1-1-1-1', result: '1-1-1-1-1' },
  { title: '1-1-1-2', result: '1-1-1-2' },
  { title: '1-1-1-3', result: '1-1-1-3' },
  { title: '2', result: '2' },
  { title: '2-2', result: '2-2' },
  { title: '3', result: '3' },
];

如果您可以帮助我生成更简洁的代码,请随时帮助我。我对javascript还是很陌生

评论
  • 卟~怕
    卟~怕 回复

    使用传播算子和递归可以看起来很干净

    function flatten(dataArray) {
      const result = [];
       dataArray.map(d => {
        const { child, ...rest } = d;
        result.push(rest);
        if(child && child.length) result.push(...flatten(child));
      });
       return result;
    }
    console.log(flatten(rawData));
    ```js