0

I'm getting this type of data from backend

let a = [
  {
    QuestionNo: "1",
    pageNumber: "1",
    response: "xyz",
    question: "What is your Name",
    publicUrl: null,
    fieldtype: "Text"
  },
  {
    QuestionNo: "2",
    pageNumber: "1",
    response: "7897897987",
    question: "Phone no",
    publicUrl: null,
    fieldtype: "Phone"
  },
  {
    QuestionNo: "1",
    pageNumber: "2",
    response: "true",
    question: "Are you employeed?",
    publicUrl: null,
    fieldtype: "Checkbox"
  },
  {
    QuestionNo: "2",
    pageNumber: "2",
    response: "2023-03-30T10:27:00.000Z",
    question: "validity",
    publicUrl: null,
    fieldtype: "Datetime"
  }
];

I need to convert it like this

[
  {
    "pageNumber": "1",
    "Questions": [
      {
        QuestionNo: "1",
        pageNumber: "1",
        response: "xyz",
        question: "What is your Name",
        publicUrl: null,
        fieldtype: "Text"
      },
      {
        QuestionNo: "2",
        pageNumber: "1",
        response: "7897897987",
        question: "Phone no",
        publicUrl: null,
        fieldtype: "Phone"
      }
    ]
  },
  {
    "pageNumber": "2",
    "Questions": [
      {
        QuestionNo: "1",
        pageNumber: "2",
        response: "true",
        question: "Are you employeed?",
        publicUrl: null,
        fieldtype: "Checkbox"
      },
      {
        QuestionNo: "2",
        pageNumber: "2",
        response: "2023-03-30T10:27:00.000Z",
        question: "validity",
        publicUrl: null,
        fieldtype: "Datetime"
      }
    ]
  }
]

My code

let finalAnswer = [];
let a = [
  {
    QuestionNo: "1",
    pageNumber: "1",
    response: "xyz",
    question: "What is your Name",
    publicUrl: null,
    fieldtype: "Text"
  },
  {
    QuestionNo: "2",
    pageNumber: "1",
    response: "7897897987",
    question: "Phone no",
    publicUrl: null,
    fieldtype: "Phone"
  },
  {
    QuestionNo: "1",
    pageNumber: "2",
    response: "true",
    question: "Are you employeed?",
    publicUrl: null,
    fieldtype: "Checkbox"
  },
  {
    QuestionNo: "2",
    pageNumber: "2",
    response: "2023-03-30T10:27:00.000Z",
    question: "validity",
    publicUrl: null,
    fieldtype: "Datetime"
  }
];

a.forEach((val) => {
  if (finalAnswer.length > 0) {
    finalAnswer.filter((v, i) => {
      if (val.pageNumber === v.pageNumber) {
       finalAnswer[i].Questions.push(val);
      } else {
        // *issue here*
        finalAnswer.push({ pageNumber: val.pageNumber, Questions: [val] });
      }
    });
  } else {
    finalAnswer.push({ pageNumber: val.pageNumber, Questions: [val] });
  }
  console.log("finalAnswer", JSON.stringify(finalAnswer));
});

Im getting issue in the issue here section.

Im getting issue in the issue here section.

Developer
  • 3
  • 2

2 Answers2

0
   const finalResult =  x.reduce((acc, curr) => {
      const { pageNumber, ...question } = curr;
      const index = acc.findIndex(page => page.pageNumber === pageNumber);
      if (index !== -1) {
        acc[index].Questions.push(question);
      } else {
        acc.push({
          pageNumber,
          Questions: [question]
        });
      }
      return acc;
    }, [])

There's no need to call the filter function to find the questions. Also, you're pushing inside the main array, instead, questions need to get appended in that page nos question key. The above code does the work!

robo
  • 89
  • 1
  • 9
0

You can try the following code. I think it may help you.

const finalAnswer = [];
const pageNumber = [];

a.forEach((val) => {
    // check whether I have inserted all the object of same page
    if (!pageNumber.includes(val.pageNumber)) {
        // insert the object into finalAnswer array (array index will be the page number)
        finalAnswer[val.pageNumber] = {
            pageNumber: val.pageNumber,
            // filter all the object as per page number and assign the field 'Questions'
            Questions: a.filter((v) => {
                return val.pageNumber == v.pageNumber;
            })
        }

        // as all object of same page number already fit into the finalAnswer array,
        // so insert the pageNumber into the pageNumber array, so that we don't need to iterate again for same pageNumber
        pageNumber.push(val.pageNumber);
    }
});

// if the pageNumber will be random, then finalArray may contains empty cells in it
// so we filter all the falsy value
const filterdFinalAnswer = finalAnswer.filter((item) => item);

// filteredFinalAnswer will be the proper array of objects
console.dir(filterdFinalAnswer, {depth: null});