Skip to content

How to map an array of objects in to a different one based on criteria? [closed]

How to map an array of objects in to a different one based on criteria?

How to convert the following array of questions in to expected array?


const questions = [
                { type: 'question', index: 1 },
                { type: 'question', index: 2 },
                { type: 'answer', index: 3 },
                { type: 'answer', index: 4 },
                { type: 'answer', index: 5 },
                { type: 'answer', index: 6 },
                { type: 'answer', index: 7 },
                { type: 'answer', index: 8 },
                { type: 'answer', index: 9 },
                { type: 'question', index: 11 },
                { type: 'answer', index: 12 },
                { type: 'answer', index: 13 },
                { type: 'question', index: 14 },
                { type: 'question', index: 15 },
                { type: 'question', index: 16 },
                { type: 'question', index: 17 },
                { type: 'answer', index: 18 },
                { type: 'answer', index: 19 },
                { type: 'question', index: 20 },
                { type: 'question', index: 21 },
                { type: 'question', index: 22 },
                { type: 'question', index: 23 },
                { type: 'question', index: 24 },
                { type: 'question', index: 25 },
                { type: 'question', index: 26 },
                { type: 'question', index: 27 },
                { type: 'question', index: 28 },
                { type: 'question', index: 100 },
                {}
          ];
              

This is the expected array of results.

                    const expected = [
                      { type: "question", index: 1, answers: [] },
                      {
                        type: "question",
                        index: 2,
                        answers: [
                          { type: "answer", index: 3 },
                          { type: "answer", index: 4 },
                          { type: "answer", index: 5 },
                          { type: "answer", index: 6 },
                          { type: "answer", index: 7 },
                          { type: "answer", index: 8 },
                          { type: "answer", index: 9 },
                        ],
                      },

                      {
                        type: "question",
                        index: 11,
                        answers: [
                          { type: "answer", index: 12 },
                          { type: "answer", index: 13 },
                        ],
                      },

                      { type: "question", index: 14, answers: [] },
                      { type: "question", index: 15, answers: [] },
                      { type: "question", index: 16, answers: [] },
                      {
                        type: "question",
                        index: 17,
                        answers: [
                          { type: "answer", index: 18 },
                          { type: "answer", index: 19 },
                        ],
                      },

                      { type: "question", index: 20, answers: []},
                      { type: "question", index: 21, answers: []},
                      { type: "question", index: 22, answers: []},
                      { type: "question", index: 23, answers: []},
                      { type: "question", index: 24, answers: []},
                      { type: "question", index: 25, answers: []},
                      { type: "question", index: 26, answers: []},
                      { type: "question", index: 27, answers: []},
                      { type: "question", index: 28, answers: []},
                      { type: 'question', index: 100, answers: [] },
                      {}
                    ];

So the idea is to have a sub array called answers in to which we push the in-between items with type === 'answer'.

So the answers should be pushed to the index n-1 with type === 'question' till the next item with type type === 'question' is encountered. If there are no answers in-between then keep the array empty.

Basicallly we look for the index and type

Update

So the way I tried was I created an array containing the indices. And each time I looped through I looked for the next maximum index and it’s type. Later based on that I tried copying the in-between indices to the previous index.

Answer

Just imagine how you would filter it as a person and then use the same way for the code.

  1. You would definitely check all the elements in that array, so you need to use a loop, and then just filter and push it into the new ‘expected’array.

This is how I would do it.

const questions = [
        { type: 'question', index: 1 },
        { type: 'question', index: 2 },
        { type: 'answer', index: 3 },
        { type: 'answer', index: 4 },
        { type: 'answer', index: 5 },
        { type: 'answer', index: 6 },
        { type: 'answer', index: 7 },
        { type: 'answer', index: 8 },
        { type: 'answer', index: 9 },
        { type: 'question', index: 11 },
        { type: 'answer', index: 12 },
        { type: 'answer', index: 13 },
        { type: 'question', index: 14 },
        { type: 'question', index: 15 },
        { type: 'question', index: 16 },
        { type: 'question', index: 17 },
        { type: 'answer', index: 18 },
        { type: 'answer', index: 19 },
        { type: 'question', index: 20 },
        { type: 'question', index: 21 },
        { type: 'question', index: 22 },
        { type: 'question', index: 23 },
        { type: 'question', index: 24 },
        { type: 'question', index: 25 },
        { type: 'question', index: 26 },
        { type: 'question', index: 27 },
        { type: 'question', index: 28 },
        { type: 'question', index: 100 }
    ];

//filtering array expected
    let expected = [];
    for (let i of questions){
        if (i.type === "question"){
            expected.push({
                type: i.type,
                index: i.index,
                answers: []
            })
        } else if (i.type === 'answer'){
            let index = expected.length - 1;
            expected[index].answers.push(i)
        }
    }



    console.log(expected);