I have two array which looks like
var monthNames = [ "January", "January", "January", "April", "April", "December", "August", "August", "November", "November", "November", "December" ];
var monthRange = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
Now I am wondering how should I work in JS so that I would get a new array back, again with 12 elements in it (one for each month) and get a count of it. So it will be like this -
[3, 0, 0, 2, 0, 0, 0, 2, 0, 0, 3, 1]
Here count is in the order of the monthRange which gives count for each month in monthNames .
So here
January : 3,
April: 2,
December: 1,
August: 2,
November: 3,
December : 1
This would do it:
var counts = monthRange.map(function(val) {
var count = 0;
for (var i = 0, j = monthNames.length; i < j; i++) {
if (val === monthNames[i]) count++;
}
return count;
});
You can map over the monthRange and then filter the monthNames to return the number of occurrences of each month:
monthRange.map(function(month) {
return monthNames.filter(function(n) { return n === month }).length;
});
Using underscore you can do it like this:
_.reduce(array, function(memo, month) {
memo[month] = (memo[month] === undefined ? 0 : memo[month]) + 1;
return memo
}, {})
In your case, it can look something like
var countMonths = function(array, m) {
return _.reduce(array, function(memo, month) {
memo[month] = (memo[month] === undefined ? 0 : memo[month]) + 1;
return memo
}, m)
}
var memo = countMonths(monthNames, {})
memo = countMonths(monthRange, memo)
Try Object to keep track of frequency:
var freq = function(list) {
var o = {};
var l = list.length;
var v;
while (v = list[--l])
o[v] = o[v] !== undefined ? o[v] + 1 : 1;
return o;
};
console.log(freq(["January", "January", "January", "April", "April", "December", "August", "August", "November", "November", "November", "December"]));
alert(JSON.stringify(freq(["apple", "mangoe", "apple"])));
Open console...
Related
I am creating dynamic labels for the chart js by supplying range of the month . It is working good if i selected start month and end month in ascending order but it is not working in the case where i selected start month = december and end month = march.
Here is my code ,
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthArr = [];
var monthn = ['December','March']; // here i give the lower and upper limit for the label
for (var i = monthNames.indexOf(monthn[0]); i <= monthNames.indexOf(monthn[1]); i++) {
monthArr.push(monthNames[i]);
}
return monthArr;
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthArr = [];
var monthn = ['December','March'];
if (monthNames.indexOf(monthn[1]) < monthNames.indexOf(monthn[0])) {
monthNames.unshift(monthNames.splice(monthNames.indexOf(monthn[0]), 1)[0]);
for (var i = monthNames.indexOf(monthn[0]); i <= diff; i++) {
monthArr.push(monthNames[i]);
}
} else {
for (var i = monthNames.indexOf(monthn[0]); i <= monthNames.indexOf(monthn[1]); i++) {
monthArr.push(monthNames[i]);
}
}
return monthArr;
Try to validate the index between the two months
Here is an example with your code and a very small changes.
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthArr = [];
var monthn = ['December','March']; // here i give the lower and upper limit for the label
// make sure that StartMonth is smaller then Endmonth
var startMonth = monthNames.indexOf(monthn[0]) <= monthNames.indexOf(monthn[1]) ? monthNames.indexOf(monthn[0]) : monthNames.indexOf(monthn[1]);
// make sure that EndMonth is bigger then StartMonth
var endMonth = monthNames.indexOf(monthn[0]) <= monthNames.indexOf(monthn[1]) ? monthNames.indexOf(monthn[1]) : monthNames.indexOf(monthn[0]);
for (var i = startMonth; i <= endMonth; i++) {
monthArr.push(monthNames[i]);
}
// Add the missing months
if (startMonth -1 >0)
{
for (var i = 0; i <= startMonth -1; i++) {
monthArr.push(monthNames[i]);
}
}
console.log(monthArr)
When I run the following:
var months = new Set(["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]);
var string = "27 - 28 August 663 CE";
var words = string.split(" ");
for (var i = 0; i < words.length - 1; i++) {
words[i] += " ";
}
var array = words;
array = $.map(array, function(value){
return value.replace(/ /g, '');
});
const dates = {
days : [],
months : [],
years : [],
suffixes : []
}
for (const word of words) {
if (months.has(word)) {
dates.months.push(word);
} else if (+word < 32) {
dates.days.push(+word);
} else if (+word < 2200) {
dates.years.push(+word);
} else if (/\w+/.test(word)) {
dates.suffixes.push(word);
}
}
console.log(array);
console.log(dates);
The output is incorrect:
Object {days: Array(2), months: Array(0), years: Array(1), suffixes: Array(2)}
While if I run:
var months = new Set(["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]);
var words = ["27","-","28","August","663","CE"];
const dates = {
days : [],
months : [],
years : [],
suffixes : []
}
for (const word of words) {
if (months.has(word)) {
dates.months.push(word);
} else if (+word < 32) {
dates.days.push(+word);
} else if (+word < 2200) {
dates.years.push(+word);
} else if (/\w+/.test(word)) {
dates.suffixes.push(word);
}
}
console.log(dates);
The output is correct:
Object {days: Array(2), months: Array(1), years: Array(1), suffixes: Array(1)}
jsFiddle
Cause youre adding a whitespace in your first code, which is completely unneccessary:
words[i] += " ";
And cause of that
"january "
isnt found in the array (or Set), as it only contains:
"january"
Hi you are using the "words" array even "array", and it has withe spaces.
This demo give expected result:
var string = "27 - 28 August 663 CE";
var months = new Set(["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]);
var words = string.split(" ");
for (var i = 0; i < words.length - 1; i++) {
words[i] += " ";
}
const dates = {
days : [],
months : [],
years : [],
suffixes : []
};
var array = words;
array = array.map(array, function(value){
return value.replace(/ /g, '');
});
console.log(array, 'array');
console.log(words);
for (word of array) {
if (months.has(word)) {
dates.months.push(word);
} else if (+word < 32) {
dates.days.push(+word);
} else if (+word < 2200) {
dates.years.push(+word);
} else if (/\w+/.test(word)) {
dates.suffixes.push(word);
}
}
console.log(dates);
I want to loop through an array.
Any index which has a length larger than 3 I want to abbreviate.
I want to place the new abbreviated months into a new array.
I want to test that it works by writing the results to the console.
I can get the code to run, however the results don't come out the way I'd hope. From my understanding the loop runs true, running the if statement, which runs true, running the code block. After that the loop should iterate and continue as long as i < months.length, but it doesn't.
var months = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthsAbbrev = [];
for (var i = 0; i < months.length; i++) {
if (months[i].length > 3) {
monthsAbbrev = months[i].slice(0, 3);
}
}
console.log(monthsAbbrev);
You can do it simply with Array.prototype.map()
var months = ["January", "Febuary"]; //sample data for better understanding
var monthsAbbrev = months.map(v => v.substr(0,3));
console.log(monthsAbbrev); //["Jan", "Feb"]
By the way you are not pushing anything into your target array in your code, that is the problem.
Beside the pushing, you can omit the check for length > 3, because slice is already doing it and that prevent to miss some month, like 'May'.
var months = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthsAbbrev = [];
for (var i = 0; i < months.length; i++) {
monthsAbbrev.push(months[i].slice(0, 3));
}
console.log(monthsAbbrev);
You need to add the months to your monthsAbbrev array. One way you can do this is by using the .push() function:
var months = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthsAbbrev = [];
for (var i = 0; i < months.length; i++) {
if (months[i].length > 3) {
monthsAbbrev.push(months[i].slice(0, 3));
}
}
console.log(monthsAbbrev);
You just had to push to the array formed. Rest you already had it.
var months = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthsAbbrev = [];
for (var i = 0; i < months.length; i++) {
if (months[i].length > 3) {
monthsAbbrev.push(months[i].slice(0, 3));
}
}
console.log(monthsAbbrev);
I am trying to get the actual month from a loop. When I step through it in developer tools and month[i] == 4 it doesn't assign actualMonth to checkMonth
Do I have to assign getMonth to month[] and then try and query the value?
var showCurrentMonth = function() {
var getMonth = new Date().getMonth();
var month = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
var actualMonth = "";
for (var i = 0; i < month.length; i++) {
var checkMonth = month[i];
console.log(month[i]);
if (getMonth == month[i]) {
actualMonth = checkMonth;
}
}
console.log(actualMonth);
}
window.addEventListener('DOMContentLoaded', showCurrentMonth, false);
Too simple?
var showCurrentMonth = function() {
var getMonth = new Date().getMonth();
var month = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
var actualMonth = month[getMonth];
console.log(actualMonth);
}
window.addEventListener('DOMContentLoaded', showCurrentMonth, false);
Change your if to if (month[getMonth] == month[i]) {
Do it like this :
var showCurrentMonth = function() {
var getMonth = new Date().getMonth();
var month = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
var actualMonth = "";
for (var i = 0; i < month.length; i++) {
var checkMonth = month[i];
console.log(month[i]);
if (month[getMonth] == month[i]) { //Compare get month like this
actualMonth = checkMonth;
}
}
console.log(actualMonth);
}
window.addEventListener('DOMContentLoaded', showCurrentMonth, false);
Your problem is that you are comparing an integer and string:
var getMonth = new Date().getMonth(); // This return number from 0 to 11
The code Date().getMonth() returns an integer, and your month list has strings on it
Your code should be:
var showCurrentMonth = function() {
var getMonth = new Date().getMonth();
var month = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
var actualMonth = month[getMonth];
console.log(actualMonth);
}
window.addEventListener('DOMContentLoaded', showCurrentMonth, false);
To get the actual month you only need to access the month list with getMonth as index
slightly Modification required
var showCurrentMonth = function() {
var getMonth = new Date().getMonth();
console.log(getMonth);
var month = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
var actualMonth = "";
for (var i = 0; i < month.length; i++) {
var checkMonth = month[i];
if (getMonth == i) {
actualMonth = checkMonth;
}
}
console.log(actualMonth);
};
window.addEventListener('DOMContentLoaded', showCurrentMonth, false);
I have to two dates from and to. I want to get all of the month names between these two dates.
Following is my code
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
function diff(from, to) {
var datFrom = new Date('1 ' + from);
var datTo = new Date('1 ' + to);
var arr = monthNames.slice(datFrom.getMonth(), datTo.getMonth() + 1);
}
above code works for following inputs
diff('September 2013', 'December 2013');
but it does not work for this
diff('September 2013', 'February 2014');
How can I make it work?
Mine is better: http://jsfiddle.net/kS73f/8/
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
function diff(from, to) {
var arr = [];
var datFrom = new Date('1 ' + from);
var datTo = new Date('1 ' + to);
var fromYear = datFrom.getFullYear();
var toYear = datTo.getFullYear();
var diffYear = (12 * (toYear - fromYear)) + datTo.getMonth();
for (var i = datFrom.getMonth(); i <= diffYear; i++) {
arr.push(monthNames[i%12] + " " + Math.floor(fromYear+(i/12)));
}
return arr;
}
console.log(diff('September 2013', 'March 2014'));
You're going to have to do a more manual method than slice. Here's a starting point you can determine how to handle cases as mentioned in comments.
function diff(from, to) {
var result = [];
var datFrom = new Date('1 ' + from);
var datTo = new Date('1 ' + to);
if(datFrom < datTo) {
var month = datFrom.getMonth();
var toMonth = datTo.getMonth() + 1 + ((datTo.getYear() - datFrom.getYear())*12); //toMonth adjusted for year
for(; month < toMonth; month++) { //Slice around the corner...
result.push(monthNames[month % 12]);
}
}
return result;
}
diff('September 2013', 'February 2014'); //=["September", "October", "November", "December", "January", "February"]
var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ];
function diff(from, to) {
var datFrom = new Date('1 ' + from);
var datTo = new Date('1 ' + to);
var arr;
if(datFrom > datTo) {
return diff(to, from);
}
var fromYear = datFrom.getFullYear();
var toYear = datTo.getFullYear();
if(fromYear === toYear) {
return monthNames.slice(datFrom.getMonth(), datTo.getMonth() + 1);
} else {
var arr = addYear(monthNames.slice(datFrom.getMonth(), new Date('1 December ' + fromYear)), fromYear);
for(var i = 1; i < (toYear - fromYear); i++) {
arr = arr.concat(addYear(monthNames, fromYear + i));
}
return arr.concat(addYear(monthNames.slice(new Date('1 January ' + fromYear).getMonth(), datTo.getMonth() + 1), toYear));
}
}
function addYear(arr, year) {
var updatedArr = [];
for(var i = 0; i < arr.length; i++) {
updatedArr[i] = arr[i] + ' ' + year;
}
return updatedArr;
}
Than try console.log(diff('September 2013', 'February 2015')) to test it.
The following modifies the original function as little as possible, if that helps the OP from a comprehension standpoint.
function diff(from, to) {
var datFrom = new Date('1 ' + from);
var datTo = new Date('1 ' + to);
var arr = monthNames.slice(datFrom.getMonth(), datTo.getMonth() + 1);
if (!arr.length) {
arr = monthNames.slice(datFrom.getMonth(), 12);
arr = arr.concat(monthNames.slice(0, datTo.getMonth() + 1));
}
return arr;
}
console.log(diff('December 2013', 'February 2014')); //["September", "October", "November", "December", "January", "February"]
Lazy answer:
var monthNames = [ "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December",
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December" ];
function diff(from, to) {
var mFrom = new Date('1 ' + from).getMonth();
var mTo = new Date('1 ' + to).getMonth();
mTo = mTo < mFrom ? mTo + 12 : mTo;
return monthNames.slice(mFrom, mTo + 1);
}
alert(diff('September 2013', 'December 2013'));
alert(diff('September 2013', 'February 2014'));