在嵌套的JSON中合并两个数组

时间:2019-05-13 08:10:06

标签: javascript typescript

我有以下JSON

const data = {
  items: [
    {
      id: 1,
      data: [
        {
          content: `Lorem ipsum dolor sit amet, consectetur adipiscing elit.`,
        },
        {
          content: `Fusce et lectus eu tellus blandit varius.`,
        },
        {
          content: `Suspendisse ut est rhoncus, pretium tellus nec, aliquet purus`,
        }
      ]
    },
    {
      id: 2,
      data: [
        {
          content: `Lorem ipsum dolor sit amet, consectetur adipiscing elit.`,
        },
        {
          content: `Fusce et lectus eu tellus blandit varius.`,
        },
        {
          content: `Suspendisse ut est rhoncus, pretium tellus nec, aliquet purus`,
        }
      ]
    }
  ]
}

如何将两个数据数组合并为一个包含6个项目的数组?每当我尝试使用reduce时,我只会得到两个数组...

2 个答案:

答案 0 :(得分:0)

您可以使用flatMap并提取data属性:

const data = {
  items: [
    {
      id: 1,
      data: [
        {
          content: `Lorem ipsum dolor sit amet, consectetur adipiscing elit.`,
        },
        {
          content: `Fusce et lectus eu tellus blandit varius.`,
        },
        {
          content: `Suspendisse ut est rhoncus, pretium tellus nec, aliquet purus`,
        }
      ]
    },
    {
      id: 2,
      data: [
        {
          content: `Lorem ipsum dolor sit amet, consectetur adipiscing elit.`,
        },
        {
          content: `Fusce et lectus eu tellus blandit varius.`,
        },
        {
          content: `Suspendisse ut est rhoncus, pretium tellus nec, aliquet purus`,
        }
      ]
    }
  ]
};

const output = data.items.flatMap(({ data }) => data);
console.log(output);

或者,而不flatMap

const data = {
  items: [
    {
      id: 1,
      data: [
        {
          content: `Lorem ipsum dolor sit amet, consectetur adipiscing elit.`,
        },
        {
          content: `Fusce et lectus eu tellus blandit varius.`,
        },
        {
          content: `Suspendisse ut est rhoncus, pretium tellus nec, aliquet purus`,
        }
      ]
    },
    {
      id: 2,
      data: [
        {
          content: `Lorem ipsum dolor sit amet, consectetur adipiscing elit.`,
        },
        {
          content: `Fusce et lectus eu tellus blandit varius.`,
        },
        {
          content: `Suspendisse ut est rhoncus, pretium tellus nec, aliquet purus`,
        }
      ]
    }
  ]
};

const output = data.items.reduce((a, { data }) => {
  a.push(...data);
  return a;
}, []);
console.log(output);

答案 1 :(得分:0)

简单用法传播运算符: 例如:

[...data.items[0].data, ...data.items[1].data]

const data = {
  items: [
    {
      id: 1,
      data: [
        {
          content: `Lorem ipsum dolor sit amet, consectetur adipiscing elit.`,
        },
        {
          content: `Fusce et lectus eu tellus blandit varius.`,
        },
        {
          content: `Suspendisse ut est rhoncus, pretium tellus nec, aliquet purus`,
        }
      ]
    },
    {
      id: 2,
      data: [
        {
          content: `Lorem ipsum dolor sit amet, consectetur adipiscing elit.`,
        },
        {
          content: `Fusce et lectus eu tellus blandit varius.`,
        },
        {
          content: `Suspendisse ut est rhoncus, pretium tellus nec, aliquet purus`,
        }
      ]
    }
  ]
};
/*using spred operator*/
console.log([...data.items[0].data, ...data.items[1].data]);