1

Need to iterate over the below JSON object and form a result json which has the unique data. Result will be basically consist of the list of questions and their choices. Please help me in this. Thanks in advance..!!

    var data = [
  {
    "category": "sports",
    "question": "Who is the best footballer?",
    "questionType": "text",
    "choices": "Messi",
    "name": "Best Footballer",
    "createdUserId": 1
  },
  {
    "category": "sports",
    "question": "Who is the best footballer?",
    "questionType": "text",
    "choices": "Ronaldo",
    "name": "Best Footballer",
    "createdUserId": 1
  },
  {
    "category": "sports",
    "question": "Who is the best footballer?",
    "questionType": "text",
    "choices": "Ibrahimovic",
    "name": "Best Footballer",
    "createdUserId": 1
  },
  {
    "category": "sports",
    "question": "Who is the top goal scorer?",
    "questionType": "text",
    "choices": "Messi",
    "name": "Best Footballer",
    "createdUserId": 1
  },
  {
    "category": "sports",
    "question": "Who is the top goal scorer?",
    "questionType": "text",
    "choices": "Ronaldo",
    "name": "Best Footballer",
    "createdUserId": 1
  },
  {
    "category": "sports",
    "question": "Who is the top goal scorer?",
    "questionType": "text",
    "choices": "Lewandoski",
    "name": "Best Footballer",
    "createdUserId": 1
  }
];

JSON to populate

{
    "name": "Best Footballer",
    "category": "sports",
    "createdUserId": "1",
    "questionList": [
        {
            "question": "Who is the best footballer?",
            "questionType": "text",
            "choices": [
                "Messi",
                "Ronaldo",
                "Ibrahimovic"
            ]
        },
        {
            "question": "Who is the top goal scorer?",
            "questionType": "text",
            "choices": [
                "Messi",
                "Ronaldo",
                "Lewandoski"
            ]
        }
    ]
}

1 Answer 1

4

Try this, I use an object qObj so that the question can be located, otherwise we have to traverse the array to find whether the question exists.

"use strict";

var data = [{
    "category": "sports",
    "question": "Who is the best footballer?",
    "questionType": "text",
    "choices": "Messi",
    "name": "Best Footballer",
    "createdUserId": 1
}, {
    "category": "sports",
    "question": "Who is the best footballer?",
    "questionType": "text",
    "choices": "Ronaldo",
    "name": "Best Footballer",
    "createdUserId": 1
}, {
    "category": "sports",
    "question": "Who is the best footballer?",
    "questionType": "text",
    "choices": "Ibrahimovic",
    "name": "Best Footballer",
    "createdUserId": 1
}, {
    "category": "sports",
    "question": "Who is the top goal scorer?",
    "questionType": "text",
    "choices": "Messi",
    "name": "Best Footballer",
    "createdUserId": 1
}, {
    "category": "sports",
    "question": "Who is the top goal scorer?",
    "questionType": "text",
    "choices": "Ronaldo",
    "name": "Best Footballer",
    "createdUserId": 1
}, {
    "category": "sports",
    "question": "Who is the top goal scorer?",
    "questionType": "text",
    "choices": "Lewandoski",
    "name": "Best Footballer",
    "createdUserId": 1
}];

var pop = {
    name: "Best Footballer",
    category: "sports",
    createdUserId: "1",
    questionList: []
};
var qObj = {};

data.forEach(function(entry) {

    if (typeof qObj[entry.question] == "undefined") {
        qObj[entry.question] = [];
    }

    qObj[entry.question].push(entry.choices);

});

for (var q in qObj) {
    if (qObj.hasOwnProperty(q)) {
        pop.questionList.push({
            question: q,
            questionType: "text",
            choices: qObj[q]
        });
    }
}

console.log(pop); // JavaScript Object
console.log(JSON.stringify(pop)); // json

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much.!! Its perfect.!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.