javascript - finding the number of occurrences of same the string stored in a array of objects -
i trying aggregate number of title names , different title sources. successful in removing duplicates array of objects , display original version of title each source single time out duplicates. have fallen short of showing detail abt how many duplicates of each title names there in json. in here have 4 each. unable total duplicates. part else
condition of ifexistflag
ive added in code. adding fiddle link
var inputjson = [{ "sources": [{ "title": "title name", "source": "sourcename1", "date": "07-05-2013 00:38:40", "link": "link 1" }, { "title": "title name", "date": "07-05-2013 00:24:17", "source": "sourcename2", "link": "link 2" } ] }, { "sources": [{ "title": "title name", "source": "sourcename1", "date": "07-05-2013 00:38:40", "link": "link 1" }, { "title": "title name", "date": "07-05-2013 00:24:17", "source": "sourcename2", "link": "link 2" } ] }, { "sources": [{ "title": "title name", "source": "sourcename1", "date": "07-05-2013 00:38:40", "link": "link 1" }, { "title": "title name", "date": "07-05-2013 00:24:17", "source": "sourcename2", "link": "link 2" } ] }, { "sources": [{ "title": "title name", "source": "sourcename1", "date": "07-05-2013 00:38:40", "link": "link 1" }, { "title": "title name", "date": "07-05-2013 00:24:17", "source": "sourcename2", "link": "link 2" } ] } ] $(document).ready(function () { var sourcelist = aggregatesource(inputjson); var sourceprint = printfinalsourcedata(sourcelist); $("#output").html(sourceprint); }); function aggregatesource(sourcearray, number) { var l = {}; var finalarray = []; (var = 0; < sourcearray.length; i++) { (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; (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 = ''; (var k = 0; k < inputarray.length; k++) { (key in inputarray[k]) { (var = 0; < 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; }
that's not json, that's javascript array. json text format represent data.
your variable ifexistflag
false. if else
, know there existed 1 item same source, , if didn't, when j
, k
equal, comparing item itself. know @ point there exists item same source, check not needed @ all.
instead of pushing objects array, put object in l
object, , when find same source increase counter. finally, return object l
instead of pushing array:
function aggregatesource(sourcearray, number) { var l = {}; (var = 0; < sourcearray.length; i++) { var s = sourcearray[i].sources; (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 have object containing objects instead of array containing object arrays of objects, there less looping needed in printfinalsourcedata
function:
function printfinalsourcedata(inputarray) { var sourcehtml = ''; (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 want show number of items found each source.
Comments
Post a Comment