2015-10-17 52 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

太感謝你了。!這是完美的。!! –