javascript - 如何在我的特定情况下形成嵌套数组的数组?

标签 javascript arrays json reactjs

我有一个特定的用例,我想将一个数组中的所有对象组合成一个新数组。所以我在我的网站上有一个表格,用户在预订时正在添加参与者。他们可以根据需要添加任意数量的参与者。我将它们作为 JSON 保存在数据库中,现在我想形成一个所有参与者的数组,以便我可以在表中显示它们。
因此,我首先从作为对象数组获得的特定列表中获取所有事务,然后循环遍历它们,只得到 transaction.attributes.protectedData.questions它持有每笔交易的参与者。
所以我的交易对象看起来像:

[
   {
      "type":"transaction",
      "attributes":{
         "createdAt":"2021-06-24T08:50:26.911Z",
         "protectedData":{
            "questions":[
               [
                  {
                     "question":"name",
                     "answer":"Mario North"
                  },
                  {
                     "question":"email",
                     "answer":"mario@gmail.com"
                  }
               ]
            ],
            "ticketType":{
               "name":"Main entry",
               "quantity":1
            }
         }
      }
   },
   {
      "type":"transaction",
      "attributes":{
         "createdAt":"2021-06-24T08:48:57.646Z",
         "protectedData":{
            "questions":[
               [
                  {
                     "question":"name",
                     "answer":"John Adkins"
                  },
                  {
                     "question":"email",
                     "answer":"john@gmail.com"
                  }
               ],
               [
                  {
                     "question":"name",
                     "answer":"Thomas Smith"
                  },
                  {
                     "question":"email",
                     "answer":"thomas@gmail.com"
                  }
               ]
            ],
            "ticketType":{
               "name":"General entry",
               "quantity":2
            }
         }
      }
   }
]
所以我需要遍历每个事务,然后遍历问题,问题数组中的每个新数组都是一个新参与者。在每个参与者中,我需要保存交易属性中的 createdAt 和 ticketType 值。
所以我的最终数组/对象需要如下所示:
[
   {
      "createdAt":"2021-06-24T08:50:26.911Z",
      "ticketType":"Main entry",
      "name":"Mario North",
      "email":"mario@gmail.com"
   },
   {
      "createdAt":"2021-06-24T08:48:57.646Z",
      "ticketType":"General entry",
      "name":"John Adkins",
      "email":"john@gmail.com"
   },
   {
      "createdAt":"2021-06-24T08:48:57.646Z",
      "ticketType":"General entry",
      "name":"Thomas Smith",
      "email":"thomas@gmail.com"
   }
]
所以我可以得到参与者列表,每个参与者都添加了 createdAt 和 ticketType。但我不知道如何才能让我的问题/答案显示为我在上面发布的我想要的对象。这就是我所拥有的:
export const denormalisedParticipantList = transactions => {
  let participants = [];

  transactions.map(transaction => {
    const createdAt = transaction.attributes.createdAt;
    const questions = transaction.attributes.protectedData?.questions;
    const ticketType = transaction.attributes.protectedData?.ticketType?.name;

    return questions.map(q => {
      // Form new participant object
      const participant = {
        createdAt,
        ticketType,
        ...Object.fromEntries(q.map(({ question, answer }) => [question, answer])),
      };

      // Push new participant
      participants.push(participant);
    });
  });

  return participants;
};
从昨晚开始,我一直在努力解决这个问题,但我无法让它发挥作用。谁能帮我弄清楚如何从我的交易对象中制作一个最终数组,我将非常感激。

最佳答案

Destructuring可以成为跟踪您在复杂对象中访问的内容的有效方法。这里有 flatMap() 返回单个展平数组和 Object.fromEntries() 映射questions数组到单个对象。

const input = [{ "type": "transaction", "attributes": { "createdAt": "2021-06-24T08:50:26.911Z", "protectedData": { "questions": [[{ "question": "name", "answer": "Mario North" }, { "question": "email", "answer": "mario@gmail.com" }]], "ticketType": { "name": "Main entry", "quantity": 1 } } } }, { "type": "transaction", "attributes": { "createdAt": "2021-06-24T08:48:57.646Z", "protectedData": { "questions": [[{ "question": "name", "answer": "John Adkins" }, { "question": "email", "answer": "john@gmail.com" }], [{ "question": "name", "answer": "Thomas Smith" }, { "question": "email", "answer": "thomas@gmail.com" }]], "ticketType": { "name": "General entry", "quantity": 2 } } } }]

const result = input.flatMap((
  {
    attributes: {
      createdAt,
      protectedData: {
        questions,
        ticketType: { name: ticketType } }
    }
  }
) => (
  questions.map(p => ({
    createdAt,
    ticketType,
    ...Object.fromEntries(p.map(({ question, answer }) => [question, answer]))
  }))
));

console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 如何在我的特定情况下形成嵌套数组的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68113588/

相关文章:

javascript - 使用比较函数排序

javascript - d3 中折线图的 x 轴上带有额外刻度的错误日期打印

javascript - Angular Material 树 - 样式可能性

c++ - 在 C++ 中调整动态数组的大小

iphone - 解析 JSON 字典/数组

json - 动态 JSON 解码 Swift 4

javascript - div 大小相对于屏幕大小

javascript - 未选中时,将对象作为绑定(bind)到数组属性的值的 VueJS 复选框不会脱离数组

c++ - shared_ptr<int> 到数组的元素 (shared_ptr<int[]>)

json - ExtJS 5.1 不会向我的服务器发送嵌套的 JSON 对象