Skip to content

Instantly share code, notes, and snippets.

@EDCHRONO-GIT
Last active August 29, 2015 14:04
Show Gist options
  • Save EDCHRONO-GIT/7d14e4206feec01e3ace to your computer and use it in GitHub Desktop.
Save EDCHRONO-GIT/7d14e4206feec01e3ace to your computer and use it in GitHub Desktop.
Eliminating Duplicates in JSON using JavaScript
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
<title>
JavaScript to find duplicates in JSON file
</title>
<script type="text/javascript" src="jquery-1.11.1.min.js">
</script>
<script type="text/javascript" charset="utf-8">
function loadJson() {
window.questionsarray=[];
$(document).ready(function()
{
duplicatecount =document.getElementById("duplicatecount");
duplicates =document.getElementById("duplicates");
// Step 1:Read Json from file
$.getJSON('data.json', function(json) {
for (i=0;i<json.Questions.Question.length;i++) {
//pushing all the questions into an array
questionsarray.push(json.Questions.Question[i].Q);
}
//Step 2:caluclating number of duplicates
var NumberofDuplicates=json.Questions.Question.length-eliminateDuplicates(questionsarray).length;
var t=document.createTextNode('Number of Duplicates='+NumberofDuplicates);
duplicatecount.appendChild(t);
//Step 3:Find Duplicate questions and display them on Page
var sorted_arr = questionsarray.sort();
var results = [];
for (var i = 0; i < questionsarray.length - 1; i++) {
if (sorted_arr[i + 1] == sorted_arr[i]) {
results.push(sorted_arr[i]);
}
}
for (var i = 0; i < NumberofDuplicates ; i++) {
// console.log("Duplicate question in Json file"+results[i]);
var p = document.createElement('p');
var myChangeText = document.createTextNode(i+1+':'+results[i]);
p.appendChild(myChangeText);
duplicates.appendChild(p);
}
}
);
}
);
}
function eliminateDuplicates(arr) {
var i,
len=arr.length,
out=[],
obj={
}
;
for (i=0;i<len;i++) {
obj[arr[i]]=0;
}
for (i in obj) {
out.push(i);
}
return out;
}
</script>
</head>
<body onload="loadJson();">
<div id="header">
<p id="duplicatecount">
</p>
<p id="duplicates">
Duplicate Questions
</p>
<div id="footer">
</div>
</body>
</html>
{
"Questions": {
"Question": [
{
"Q": "Which of the following is a responsibility of Supplier Management?",
"o1": "Development, negotiation and agreement of Operational Level Agreements (OLAs)",
"o2": "Development, negotiation and agreement of the Service Portfolio",
"o3": "",
"a": "Development, negotiation and agreement of contracts",
"ua": "NA"
},
{
"Q": "Which of the following does the Availability Management process include? 1. Ensuring services are able to meet availability targets 2. Monitoring and reporting actual availability 3. Improvement activities, to ensure that services continue to meet or exceed their availability goals",
"o1": "1 and 2 only",
"o2": "1 and 3 only",
"o3": "",
"a": "All the Above",
"ua": "NA"
},
{
"Q": "Which of the following is NOT a responsibility of the Service Design Manager?",
"o1": "Take the overall Service Strategies and ensure they are reflected in the Service Design process and the service designs that are produced",
"o2": "Measuring the effectiveness and efficiency of Service Design and the supporting processes",
"o3": "",
"a": "Design and maintain all necessary Service Transition packages",
"ua": "NA"
},
{
"Q": "Which of the following options is a hierarchy that is used in Knowledge Management?",
"o1": "Knowledge - Wisdom - Information - Data",
"o2": "Wisdom - Information - Data - Knowledge",
"o3": "",
"a": "Data - Information - Knowledge - Wisdom",
"ua": "NA"
},
{
"Q": "Which of the following statements is CORRECT?",
"o1": "The Service Knowledge Management System is part of the Configuration Management System",
"o2": "The Configuration Management System is part of the Configuration Management Database",
"o3": "",
"a": "The Configuration Management System is part of the Service Knowledge Management system",
"ua": "NA"
},
{
"Q": "Which of the following is one of the PRIMARY objectives of Service Strategy?",
"o1": "To design and build processes that will meet business needs",
"o2": "To provide detailed specifications for the design of IT services",
"o3": "",
"a": "To transform Service Management into a strategic asset",
"ua": "NA"
},
{
"Q": "Which of the following is the BEST description of a Service-based Service Level Agreement (SLA)?",
"o1": "An agreement that covers service specific issues in a multi-level SLA structure",
"o2": "An agreement that covers one service for a single customer",
"o3": "",
"a": "An agreement that covers one service for all users of that service",
"ua": "NA"
},
{
"Q": "Event Management, Problem Management, Access Management and Request Fulfilment are part of which stage of the Service Lifecycle?",
"o1": "Service Strategy",
"o2": "Service Design",
"o3": "",
"a": "Service Operation",
"ua": "NA"
},
{
"Q": "Which of the following is one of the PRIMARY objectives of Service Strategy?",
"o1": "To design and build processes that will meet business needs",
"o2": "To provide detailed specifications for the design of IT services",
"o3": "",
"a": "To transform Service Management into a strategic asset",
"ua": "NA"
},
{
"Q": "Which of the following is the BEST description of a Service-based Service Level Agreement (SLA)?",
"o1": "An agreement that covers service specific issues in a multi-level SLA structure",
"o2": "An agreement that covers one service for a single customer",
"o3": "",
"a": "An agreement that covers one service for all users of that service",
"ua": "NA"
},
{
"Q": "Event Management, Problem Management, Access Management and Request Fulfilment are part of which stage of the Service Lifecycle?",
"o1": "Service Strategy",
"o2": "Service Design",
"o3": "",
"a": "Service Operation",
"ua": "NA"
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment