删除JSON中的重复对象并形成一个数组

Remove duplicate objects in JSON and form an array

本文关键字:数组 一个 JSON 对象 删除      更新时间:2023-09-26

需要遍历下面的JSON对象并形成一个具有唯一数据的结果JSON。结果将基本上由问题列表和他们的选择组成。请帮帮我。提前感谢!!

    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填充

{
    "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"
            ]
        }
    ]
}

试试这个,我使用对象 qObj以便可以定位问题,否则我们必须遍历数组以查找问题是否存在。

"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