2015-10-17 73 views
1

需要遍历下面的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" 
      ] 
     } 
    ] 
} 

回答

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

+0

太感谢你了。!这是完美的。!! –