删除JSON中的重复对象并形成一个数组
Remove duplicate objects in JSON and form an array
需要遍历下面的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
相关文章:
- Javascript(Angular)从一个对象数组到第二个数组查找值
- 根据id将json数组组合为一个json数组
- JavaScript数组包含一个值
- 对一个对象使用reduce可以返回一个没有't在数组中包含目标字母
- jQuery$.inArray()总是返回-1和一个对象数组
- 在数组中的一个元素上设置多个值
- javascript处理一个对象数组以获得一个新的对象数组
- 作为一个二维数组,从ajax接收
- 你能用来自数组的属性名称生成一个对象吗
- 多维关联数组的最后一个索引
- 如何创建一个方法来验证数组的范围
- 循环以检查数组中的最后一个图像
- 在Javascript中将一个值和字符串数组转换为if语句
- 算法:从数组(javascript/angular)中按当前日期获取上一个和下一个事件
- 如何将一个对象添加到每个对象数组中
- 如何创建一个谷歌地图地理坐标数组
- 如何从另一个带下划线的数组中筛选带元素的数组
- 使用window.location.htm和匹配的URL数组(一个用于桌面,一个用于移动)将桌面网站重定向到移动
- Javascript排序多维数组-一个完整的例子
- 刽子手的游戏.2数组.一个需要相应地更新另一个