Reputation: 2240
I was trying to aggregate the number of title names and from different title sources.
I was successful in removing duplicates from array of objects and display only original version of title from each source only single time with out any duplicates. But I have fallen short of showing the detail abt how many duplicates of each title names were there in the json. Like in here we have 4 from each. But I was unable to get the total duplicates. The part is the else
condition of ifExistFlag
that Ive added in the code.
I am also adding a fiddle link
var inputJson = [{
"Sources": [{
"title": "Title Name",
"source": "SourceName1",
"date": "07-05-2013 00:38:40",
"link": "Link to 1"
}, {
"title": "Title Name",
"date": "07-05-2013 00:24:17",
"source": "SourceName2",
"link": "Link to 2"
}
]
}, {
"Sources": [{
"title": "Title Name",
"source": "SourceName1",
"date": "07-05-2013 00:38:40",
"link": "Link to 1"
}, {
"title": "Title Name",
"date": "07-05-2013 00:24:17",
"source": "SourceName2",
"link": "Link to 2"
}
]
}, {
"Sources": [{
"title": "Title Name",
"source": "SourceName1",
"date": "07-05-2013 00:38:40",
"link": "Link to 1"
}, {
"title": "Title Name",
"date": "07-05-2013 00:24:17",
"source": "SourceName2",
"link": "Link to 2"
}
]
}, {
"Sources": [{
"title": "Title Name",
"source": "SourceName1",
"date": "07-05-2013 00:38:40",
"link": "Link to 1"
}, {
"title": "Title Name",
"date": "07-05-2013 00:24:17",
"source": "SourceName2",
"link": "Link to 2"
}
]
}
]
$(document).ready(function () {
var sourceList = AggregateSource(inputJson);
var sourcePrint = printFinalSourceData(sourceList);
$("#output").html(sourcePrint);
});
function AggregateSource(sourceArray, number) {
var l = {};
var finalArray = [];
for (var i = 0; i < sourceArray.length; i++) {
for (var j = 0; j < sourceArray[i].Sources.length; j++) {
if (l.hasOwnProperty(sourceArray[i].Sources[j].source) == false) {
l[sourceArray[i].Sources[j].source] = [];
var m = {};
m["title"] = sourceArray[i].Sources[j].title;
m["link"] = sourceArray[i].Sources[j].link;
m["date"] = sourceArray[i].Sources[j].timeStamp;
m["count"] = 1;
l[sourceArray[i].Sources[j].source].push(m);
} else {
var ifExistFlag = true;
for (var k = 0; k < l[sourceArray[i].Sources[j].source].length; k++) {
if (l[sourceArray[i].Sources[j].source][k]["title"] == sourceArray[i].Sources[j].title) {
ifExistFlag = false;
}
}
if (ifExistFlag == true) {
m["title"] = sourceArray[i].Sources[j].title;
m["date"] = sourceArray[i].Sources[j].timeStamp;
m["link"] = sourceArray[i].Sources[j].link;
l[sourceArray[i].Sources[j].source].push(m);
} else {
// alert(l[sourceArray[i].Sources[j].source][j]["count"])
}
}
}
}
finalArray.push(l);
return finalArray;
}
function printFinalSourceData(inputArray) {
var sourceHTML = '';
for (var k = 0; k < inputArray.length; k++) {
for (key in inputArray[k]) {
for (var i = 0; i < inputArray[k][key].length; i++) {
sourceHTML += "<span style='width:80%; float:left; font-size:14px;'><a href='" + inputArray[k][key][i].link + "' target='_blank' style='text-decoration:none; color:#333333; font-weight:bold;'>" + inputArray[k][key][i].title + "</a></span><br><span style='width:80%;float:left; font-size:10px; color: #3f4041;'>" + key.split("_").join(" "); + "</span><br>";
}
}
}
return sourceHTML;
}
Upvotes: 0
Views: 134
Reputation: 700880
That's not JSON, that's a Javascript array. JSON is a text format to represent data.
Your variable ifExistFlag
will always be false. If you get into the else
, you already know that there already existed one item with the same source, and even if it didn't, when j
and k
are equal, you will be comparing the item to itself. As you already know at that point that there already exists an item with the same source, the check is not needed at all.
Instead of pushing objects into an array, put the object in the l
object, and when you find another with the same source you just increase the counter. Finally, just return the object l
instead of pushing it all by itself into an array:
function AggregateSource(sourceArray, number) {
var l = {};
for (var i = 0; i < sourceArray.length; i++) {
var s = sourceArray[i].Sources;
for (var j = 0; j < s.length; j++) {
if (!l.hasOwnProperty(s[j].source)) {
l[s[j].source] = {
title: s[j].title,
link: s[j].link:
date: s[j].timeStamp,
count: 1
};
} else {
l[s[j].source].count++;
}
}
}
return l;
}
As you have an object containing objects instead of an array containing an object with arrays of objects, there is much less looping needed in the printFinalSourceData
function:
function printFinalSourceData(inputArray) {
var sourceHTML = '';
for (key in inputArray) {
sourceHTML +=
"<span style='width:80%; float:left; font-size:14px;'>" +
"<a href='" + inputArray[key].link + "' target='_blank' style='text-decoration:none; color:#333333; font-weight:bold;'>" +
inputArray[key].title +
"</a></span><br>" +
"<span style='width:80%;float:left; font-size:10px; color: #3f4041;'>" +
key.split("_").join(" "); +
"</span><br>";
}
return sourceHTML;
}
You can use inputArray[key].count
if you want to show the number of items found for each source.
Upvotes: 1