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

时间:2015-10-17 22:56:23

标签: javascript json

需要迭代以下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"
            ]
        }
    ]
}

1 个答案:

答案 0 :(得分:4)

试试这个,我使用对象 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