Count occurrences from object - javascript

Im making an api call and sorting data that I wanted from it. The only problem is that I have to count the date existed.
Currently I have successfully displayed the data that I want. Which looks like this.
2017-06-09T17:49:49.975+12:00
2017-07-13T08:59:20.023+12:00
2017-05-29T14:17:18.148+12:00
2017-05-21T22:19:07.951+12:00
2017-06-09T13:59:50.464+12:00
2017-06-13T08:18:30.340+12:00
2017-07-27T12:41:17.423+12:00
Now I want to count how many has the same dates. Format would be something like,
{ date = 2017-06-09T17:49:49.975+12:00, count = 2, date = 2017-07-13T08:59:20.023+12:00, count = 1... and so on... }
I've tried few codes from here which didn't work for me.
Here's what I tried recently,
https://codepen.io/anon/pen/gxyZXm

Here is my version of the solution.
Codepen Demo
JS:
console.clear();
var counters = [];
$.ajax({
async: true,
 dataType: "json",
 url: "https://api.myjson.com/bins/n1cwx",
 success: function(data) {
   $.each(data, function (i, object) {
console.log(object.last_activity_at);
if (!object.last_activity_at) return;
var count= 0;
var arr = [];
for (var key in data){
var date1 = new Date(data[key].last_activity_at).setHours(0,0,0,0);
var date2 = new Date(object.last_activity_at).setHours(0,0,0,0);
if(date1 === date2){
count += 1;
}
};
$("body").append("<p>" + count + "</p>");
console.log(count);
console.log(arr);
$("body").append("<p>" + object.last_activity_at + "</p>");
});
 }
});

I've updated your pen to work somewhat, here's the key thing I did.
Count the occurrences of each timestamp
let keys = Object.keys(data)
let dateCount = keys.reduce((acc, val) => {
if (data[val].created_at in acc) {
acc[data[val].created_at]++
} else {
acc[data[val].created_at] = 1
}
return acc
}, {})
dateCount now has the amount of times each date appeared. From there, it's pretty simple. I create a count variable that grabs the count, or if it doesn't exist, set it to 0.
   $.each(data, function (i, object) {
if (!object.last_activity_at) return;
const count = dateCount[object.last_activity_at] ? dateCount[object.last_activity_at] : 0
$("body").append("<p>" + object.last_activity_at + " " + count + "</p>");
https://codepen.io/anon/pen/brXLOM?editors=0010

Simply do this:
var counts = {};
dateArray.forEach(function(x) {
counts[x] = (counts[x] || 0) + 1;
});
console.log(counts);

Keep inner loop to outside of loop like below
// initialize the counters outside of the loop
var counters = [];
$.each(data, function (i, object) {
if (!object.last_activity_at) return true;
counters[object] =counters[object] ? counters[object] + 1 : 1;
});
// keep next loop out side because you will get multiple line of same date with different number
// now returning the same kind of dates
var keys = Object.keys(counters);
$(keys).each(function(index,key){
$("body").append("<p> "+key+":" + counters[key] + "</p>");
})

Related

javascript using reduce function

I have the below array
["0,5,p1", "24,29,p2", "78,83,p2", "78,83,p3", "162,167,p2" ]
i want the output as ["5,p1","10,p2","5,p3"] , so p1..3 are video files paying time with start and end time . 0,5 mean p1 profile played for 5 sec and so on.
I want to know what profile take what time in total using ECMA script map,reduce function. Here is what i tried but it doesnt work:
var ca = uniqueArray.reduce(function(pval, elem) {
var spl = elem.split(',');
var difference = Math.round(spl[1] - spl[0]);
return difference;
},elem.split(',')[3]);
I dont think it can be done in one pass, but I could be wrong. I'd go for a 2 step...
Reduce the array to get unique map of pX values
Map the result back to an array in the required format
var input = ["0,5,p1", "24,29,p2", "78,83,p2", "78,83,p3", "162,167,p2" ]
var step1 = input.reduce(function(p,c){
var parts = c.split(",");
if(!p[parts[2]])
p[parts[2]] = 0;
p[parts[2]] += parseInt(parts[1],10) - parseInt(parts[0],10);
return p;
},{});
var result = Object.keys(step1).map(function(e){
return step1[e] + "," + e;
});
console.log(result);
You could use es6 map:
arrayWithNumbers.map(a => {var spl = a.split(','); return (spl[1] - spl[0]) + "," + spl[2]})
For a single loop approach, you could use a hash table for same third parts, like 'p1'. If a hash is given, then update the value with the actual delta.
var array = ["0,5,p1", "24,29,p2", "78,83,p2", "78,83,p3", "162,167,p2"],
hash = Object.create(null),
result = array.reduce(function(r, a) {
var parts = a.split(','),
delta = parts[1] - parts[0],
key = parts[2];
if (!(key in hash)) {
hash[key] = r.push([delta, key].join()) - 1;
return r;
}
r[hash[key]] = [+r[hash[key]].split(',')[0] + delta, key].join();
return r;
}, []);
console.log(result);
I have updated the code. Please check now.
var ca = ["0,5,p1", "24,29,p2", "78,83,p2", "78,83,p3", "162,167,p2" ] .reduce(function(result, elem) {
var spl = elem.split(',');
var difference = Math.round(spl[1] - spl[0]);
var found = false
for (var i = 0 ; i < result.length; i++) {
if (result[i].split(',')[1] == spl[2]) {
result[i] = parseInt(result[i].split(',')[0]) + difference+","+spl[2];
found = true;
}
}
if (!found) result.push(difference+","+spl[2]);
return result;
},[]);
console.log("modified array",ca);

javascript - sorting array by order of another array

My goal is to sort this div
<div id="myDiv">3xOrange;2xBlue;1xRed;1xRed;1xRed;1xOrange;2xBlue;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;2xOrange;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;20xBlue;33xRed;20xBlue;33xRed;2xBlue;3xRed;51xBlue;51xRed;</div>
by another div in this order
<div id="array"> Blue: 1,Red: 2,Orange: 3, </div>
So my Wanted result is to get result like this
2xBlue;1xBlue;1xBlue;2xBlue;3xRed;3xRed;1xRed;1xRed;2xOrange;3xOrange ......
I aware for the first div needs to be used string split something like this .split('x')[1];
So far I have this code:
var init_arr;
var scorer;
window.onload=function() {
scorer=document.getElementById("array").innerHTML;
init_arr = document.getElementById("myDiv").innerHTML;
var final_arr = init_arr.sort(function(a,b) {
return scorer[a]-scorer[b];
});
}
alert(final_arr);
but getting error TypeError: init_arr.sort is not a function I guess init_arr and scorer are objects not strings
Please Help
This answer deletes the rest of the strings with ; or ,, treats array like a part of a JSON string, and sort with the part after the x.
window.onload = function() {
var init_arr = document.getElementById("myDiv").innerHTML.split(';'),
scorer = JSON.parse('{' + document.getElementById("array").innerHTML + '}');
init_arr.sort(function(a, b) {
var aa = a.split('x')[1],
bb = b.split('x')[1];
return scorer[aa] - scorer[bb];
});
alert(init_arr);
};
<div id="myDiv">3xOrange;2xBlue;1xRed;1xRed;1xRed;1xOrange;2xBlue;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;2xOrange;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;20xBlue;33xRed;20xBlue;33xRed;2xBlue;3xRed;51xBlue;51xRed</div>
<div id="array">"Blue": 1,"Red": 2,"Orange": 3</div>
But I really suggest to use real arrays for data and objects for sorting order. And not any parts of HTML code.
Well, I felt dummy after playing around to help you after seeing the first answer, but here it goes.
<div id="myDiv">3xOrange;2xBlue;1xRed;1xRed;1xRed;1xOrange;2xBlue;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;2xOrange;3xRed;1xBlue;1xRed;2xBlue;3xRed;1xBlue;1xRed;20xBlue;33xRed;20xBlue;33xRed;2xBlue;3xRed;51xBlue;51xRed;</div>
<div id="array"></div>
var init;
var final;
var scorer;
scorer = document.getElementById("array");
init = document.getElementById("myDiv");
init = init.textContent.split(/\;/);
init = init.filter(function(item) {
return item.length > 0;
})
.map(function(item) {
item = item.split(/x/);
var obj = {
color: item[1],
amount: parseInt(item[0])
}
return obj;
});
final = init.reduce(function(scored, item) {
if(scored[item.color] === undefined) {
scored[item.color] = 0;
}
scored[item.color] += item.amount;
return scored;
}, {});
final = Object.keys(final)
.sort(function(item1, item2) {
return final[item1].amount - final[item2].amount;
})
.map(function(key) {
return key + ' :' + final[key];
});
scorer.textContent = final.join(', ');
At least it was funny to play with map, filter, reduce and sort
This is the sort of thing you could do:
function sort() {
var scorer;
var scorerLookup;
var sortedLookup;
//First we figure out the sort order
scorer = document.getElementById("array").innerHTML.split(',');
scorer.sort(function(a, b) {
aVal = parseInt(a.split(':')[1].trim());
bVal = parseInt(b.split(':')[1].trim());
return aVal - bVal;
});
console.log(scorer);
//Now put the sort order into an object so we can easily lookup values
scorerLookup = {};
for (var i = 0; i < scorer.length; i++) {
var tempVal = scorer[i].split(':');
scorerLookup[tempVal[0].trim()] = parseInt(tempVal[1].trim());
}
console.log(scorerLookup);
//Now sort the main list
init_arr = document.getElementById("myDiv").innerHTML.split(';');
init_arr.sort(function(a, b) {
aVal = scorerLookup[a.split('x')[1]];
bVal = scorerLookup[b.split('x')[1]];
return aVal - bVal;
});
console.log(init_arr);
}
window.onload=sort();
It needs more error trapping really (for blank values, etc) - but it should give you the general idea.

javascript: limit number of items

sorry but I'm new in javascrit.
I'm writing a simple rss reader, but I want to limit the number of feeds to 5 items, because the my target site can have 100 or more.
Here the code:
$("#mainPage").live("pageinit", function () {
$("h1", this).text(title);
$.get(RSS, {}, function (res, code) {
var xml = $(res);
var items = xml.find("item");
var items = $items.length && i < 5; i++); // here the problem!!
var entry = "";
$.each(items, function (i, v) {
entry = {
title:$(v).find("title").text(),
link:$(v).find("link").text(),
description:$.trim($(v).find("encoded").text()),
category:$.trim($(v).find("category").text()),
date:$(v).find("pubDate").text().substr(0,16),
autor:$(v).find("creator").text()
};
entries.push(entry);
});
var s = '';
$.each(entries, function(i, v) {
s += '<li>' + v.title + '<br><i>' + v.autor + ' - ' + v.date + '</i></li>';
});
$("#linksList").append(s);
$("#linksList").listview("refresh");
});
});
The problem is that when I try to limit the number of items adding
var items = $items.length && i < 5; i++);
the javascript stop to works. :-(
How to do it?
var items = $items.length && i < 5; i++); is invalid I think you want to
var items = items.slice(0, 4); is what you want.
https://api.jquery.com/slice/ (as pointed out to me, it's a jQuery object)
The jQuery object itself behaves much like an array; it has a length
property and the elements in the object can be accessed by their
numeric indices [0] to [length-1]. Note that a jQuery object is not
actually a Javascript Array object, so it does not have all the
methods of a true Array object such as join().
Because Javascript is 0-based (starts to count from 0) you want element 0 to 4, in order to get the first 5 elements.
You can use lt(n) function
var items = xml.find("item:lt(5)");

Array text to numbers, find matching values and sort

I have an array which looks like this:
["1,8", "4,6,8", "8,9", "6,9"]
1/ I would like to turn it in to this
[1,8,4,6,8,8,9,6,9]
2/ I would then like to find matching values, by looking for the most number:
[8]
This first has been solved with this:
var carArray = ["1,8", "4,6,8,7,7,7,7", "8,9", "6,9"];
//1) create single array
var arr = carArray.join().split(',');
//2) find most occurring
var counts = {}; //object to hold count for each occurence
var max = 0, maxOccurring;
arr.forEach(function(el){
var cnt = (counts[el] || 0); //previous count
counts[el] = ++cnt;
if(cnt > max && cnt > 1){ //only register if more than once (cnt>1)
max=cnt;
maxOccurring = el;
}
});
if(maxOccurring){
//there was an element more than once, maxOccuring contains that element
setResult('Most occuring: ' + maxOccurring + ' (' + max + ' times)');
}
else{
//3)/4) ???
setResult('sorting?');
}
//below is only for test display purposes
function setResult(res){
console.log(res);
}
3/ If the are no matching values like this
[1,8,4,6,5,7]
4/ Then I need to compare this array to another array, such as this
[6,7,4,1,2,8,9,5]
If the first number in <4> array above appears in <3> array, then get that number, ie in the above example I need to get 6. The <4> array will be static values and not change. The numbers is <3> will be dynamic.
EDIT Not the most elegant of answers, but I do have something working now. I didn't compare the original array directly with the second array, instead used simple if/else statements to do what I needed:
var carArray = ["1,5", "4", "8,2", "3,9,1,1,1"];
//1) create single array
var arr = carArray.join().split(',');
//2) find most occurring
var counts = {}; //object to hold count for each occurence
var max = 0, maxOccurring;
arr.forEach(function(el){
var cnt = (counts[el] || 0); //previous count
counts[el] = ++cnt;
if(cnt > max && cnt > 1){ //only register if more than once (cnt>1)
max=cnt;
maxOccurring = el;
}
});
if(maxOccurring){
//there was an element more than once, maxOccuring contains that element
console.log('Most occuring: ' + maxOccurring + ' (' + max + ' times)');
console.log(maxOccurring);
}
else {
// If not occuring, match from a list
if(jQuery.inArray("6", arr) !== -1) { console.log('6'); }
else if(jQuery.inArray("9", arr) !== -1) { console.log('9'); }
else if(jQuery.inArray("7", arr) !== -1) { console.log('7'); }
else if(jQuery.inArray("5", arr) !== -1) { console.log('5'); }
else if(jQuery.inArray("4", arr) !== -1) { console.log('4'); }
else if(jQuery.inArray("1", arr) !== -1) { console.log('1'); }
else { console.log('not found'); }
}
Example Fiddle
Step 1 is fairly easy by using javascript's join and split methods respectively:
var arr = carArray .join().split(',');
For step 2, several methods can be used, the most common one using an object and using the elements themselves as properties. Since you only need to get the most occurring value if there is a reoccurring value, it can be used in the same loop:
var counts = {}; //object to hold count for each occurence
var max = 0, maxOccurring;
arr.forEach(function(el){
var cnt = (counts[el] || 0); //previous count
counts[el] = ++cnt;
if(cnt > max && cnt > 1){ //only register if more than once (cnt>1)
max=cnt;
maxOccurring = el;
}
});
After the above, the variable maxOccurring will contain the reoccurring value (if any) and max will contain the times it occured
For step 4 the easiest way is to loop through the compare array and get the element that occurs in the input array:
var cmpArr = ['6','7','4','1','2','8','9','5'];
//find the first occurrence inside the cmpArr
res = function(){ for(var i= 0 ; i < cmpArr.length; i++){ if(arr.indexOf(cmpArr[i]) !== -1)return cmpArr[i];}}();
The above uses an in place function which is called immediately to be able to use return. You could also just use a loop and assign res when found, then break from the loop.
Last update, an alternate fiddle where the above is converted to a single function: http://jsfiddle.net/v9hhsdny/5/
Well first of all the following code results in four matching answers since the jQuery selectors are the same.
var questionAnswer1 = $(this).find('input[name=questionText]').val();
var questionAnswer2 = $(this).find('input[name=questionText]').val();
var questionAnswer3 = $(this).find('input[name=questionText]').val();
var questionAnswer4 = $(this).find('input[name=questionText]').val();
var carArray = [questionAnswer1, questionAnswer2, questionAnswer3, questionAnswer4];
You could use the eq(index) method of jQuery to select the appropriate element. However having 4 inputs with the same name is a bad practice.
Well lets say that the carArray has 4 different values which all consist out of comma separated numbers. You could then do the following:
var newArr = [];
carArray.forEach(function(e) {
e.split(",").forEach(function(n) {
newArr.push(n);
});
});
Well then we got to find the most occurring number. JavaScript doesn't have any functions for that so we will have to find an algorithm for that. I found the following algorithm on this stackoverflow page
var count = function(ary, classifier) {
return ary.reduce(function(counter, item) {
var p = (classifier || String)(item);
counter[p] = counter.hasOwnProperty(p) ? counter[p] + 1 : 1;
return counter;
}, {})
}
var occurances = count(newArr);
It isn't clear to me what you're trying to do in step 3 and 4, so can't answer those at the moment.
var ary = ["1,8", "4,6,8", "8,9", "6,9"];
var splitted = ary.reduce(function(acc, item) {
return acc.concat(item.split(','));
}, []);
var occurences = splitted.reduce(function(acc, item) {
if (!acc.hasOwnProperty(item)) acc[item] = 0;
acc[item] += 1;
return acc;
},{}),
biggest = Object.keys(occurences).reduce(function (acc, key) {
if (occurences[key] > acc.occurences) {
acc.name = key;
acc.occurences = occurences[key];
}
return acc;
},{'name':'none','occurences':0}).name;
var vals=["1,8", "4,6,8", "8,9", "6,9"];
// 1) turn into number array
var arrNew=[];
for(var i=0; i<vals.length; i++)
{
arrLine=vals[i].split(",");
for (var j=0;j<arrLine.length;j++) { arrNew.push (parseInt(arrLine[j])) }
}
//result:
alert(arrNew.join(";");
// 2) find most common
var found=[];
for(var i=0; i<arrNew.length; i++) {
// make an array of the number of occurrances of each value
if (found["num"+newArray[i]]) {
found["num"+newArray[i]] ++ ;
} else {
found["num"+newArray[i]]=1;
}
}
var mostCommon={count:0,val:"ROGUE"};
for (x in found) {
if (found[x] > mostCommon.count) {
mostCommon.count=found[x].count;
mostCommon.val=x;
}
}
// result :
alert(mostCommon.val);
//3) not quite sure what you meant there
// 4) unique values:
// at this point the 'found' list contains unique vals
var arrUnique=[];
for (x in found) {
arrUnique.push[x];
}
// result :
alert(arrUnique.join(";"))
//sort:
arrUnique.sort(function(a, b){return a-b});
(This won't work in most browsers) but on a side note, when ES6 becomes widely supported, your solution could look like this:
var arr1 = ["1,8", "4,6,8", "8,9", "6,9"];
var arr2 = arr1.join().split(',');
var s = Array.from(new Set(arr2)); //Array populated by unique values, ["1", "8", "4", "6", "9"]
Thought you might like to see a glimpse of the future!
1.
var orgArray = ['1,8', '4,6,8', '8,9', '6,9'];
var newArray = [];
for (var i in orgArray) {
var tmpArray = orgArray[i].split(',');
for (var j in tmpArray) {
newArray.push(Number(tmpArray[j]));
}
}
2.
var counts = {};
var most = null;
for (var i in newArray) {
var num = newArray[i];
if (typeof counts[num] === 'undefined') {
counts[num] = 1;
} else {
++(counts[num]);
}
if (most == null || counts[num] > counts[most]) {
most = num;
} else if (most != null && counts[num] === counts[most]) {
most = null;
}
}
I don't understand the question 3 and 4 (what "unique order" means) so I can't answer those questions.

counting unique values with it's associated count and total number of values

It may be simple but I am struck now. I have an array
items=["shelter","tent","shelter","injured","displaced","displaced"]
and it's values with it's respective value as
magnitude = [5,12,6,9,13,2]
Now,the task is to add item_name with it's respective total magnitude and number of its occurrence as:
shelter for 11 people,
tent for 12 people,
9 people injured,
15 people displaced
I tried with some loops and conditions but I am tired now. Thank you for your help in advance.
You could start by creating an object like this:
var map = {};
for (var i = 0, count = items.length; i < count; i++) {
map[items[i]] = (map[items[i]] || 0) + magnitude[i];
}
jsfiddle
You can view the values like this:
for (var item in map) {
console.log(item + ': ' + map[item]);
}
Or just access them individually, like this:
console.log('shelter for ' + map['shelter'] + ' people');
console.log('tent for ' + map['tent'] + ' people');
console.log(map['injured'] + ' people injured');
console.log(map['displaced'] + ' people displaced');
You could do something like
var msgs = {
shelter: 'shelter for {count} people',
tent: 'tent for {count} people',
injured: '{count} people injured',
displaced: '{count} people displaced'
};
tems = ["shelter", "tent", "shelter", "injured", "displaced", "displaced"];
magnitude = [5, 12, 6, 9, 13, 2];
var tmp = {};
for (var i = 0; i < tems.length; i++) {
tmp[tems[i]] = (tmp[tems[i]] || 0) + 1;
}
var array = [];
for (var key in tmp) {
array.push(msgs[key].replace('{count}', tmp[key]))
}
console.log(array)
Demo: Fiddle
If you want do that you can use $.each, like this:
var items=["shelter","tent","shelter","injured","displaced","displaced"]
var magnitudes = [5,12,6,9,13,2]
var map = new Map();
$.each(items,function(key,value) {
console.log(value);
var element = map.get(value);
if(element == undefined)
element = 0;
var element = element + magnitudes[key];
map.set(value,element);
})
console.log(map);
Here we are looping in items and putting to a map, if we alredy have the value we add te new value to the old.
Hope it helps.

Categories