Click here to Skip to main content
15,910,872 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an array where I'm displaying the highest integer value of 'visitors'. I would also like to display the value of 'day' that is associated with the highest integer of 'visitors'. Right now my code displays the correct 'visitors' value but displays all the values for 'day'. Below is what I tried. Thanks for any help.

What I have tried:

JavaScript
<!doctype html>

<meta charset="utf-8">
<title>Days

function addLoadEvent(func) { 
	  var oldonload = window.onload; 
	  if (typeof window.onload != 'function') { 
	    window.onload = func; 
	  } else { 
	    window.onload = function() { 
	      if (oldonload) { 
	        oldonload(); 
	      } 
	      func(); 
	    } 
	  } 
	} 
	
	addLoadEvent(mostPopularDays);
	addLoadEvent(function() { 
	  
	}); 
 		 
	  function mostPopularDays(week) {
			  var popularDays = [ {day: 'Sunday: ', visitors:'25'}, 
								  {day: 'Monday: ', visitors: '13'}, 
								  {day: 'Tuesday: ', visitors: '3'}, 
								  {day: 'Wednesday: ', visitors: '9'}, 
								  {day: 'Thursday: ', visitors: '6'}, 
								  {day: 'Friday: ', visitors: '16'}, 
								  {day: 'Saturday: ', visitors: '20' }		   	 
		   ];
		   
		   
var weekDay = popularDays.map(function(o) {
  return o.day;
});
  
var maximum = popularDays.map(function(x) {
  return x.visitors;
});

var maxValue = Math.max.apply(Math, maximum);

var index = maximum.indexOf(maxValue + '');

var value = weekDay.valueOf(maxValue);

 document.write(maxValue + ' : ' + index + '<br />');			
 document.write(value);				
}


HTML
<p id="lblmostPopularDays">lblmostPopularDays</p>
Posted
Updated 16-Jan-18 15:20pm
v2
Comments
Dotnet_Dotnet 16-Jan-18 20:45pm    
try

var maximum = popularDays.map(function(x) {
document.write(x.day);
return x.visitors;
});

1 solution

You can try something like below. First find the max visitors count, then search for the day based on the count number.

JavaScript
mostPopularDays(10);

function mostPopularDays(week) {
    var popularDays = [{
            day: 'Sunday: ',
            visitors: '25'
        },
        {
            day: 'Monday: ',
            visitors: '13'
        },
        {
            day: 'Tuesday: ',
            visitors: '3'
        },
        {
            day: 'Wednesday: ',
            visitors: '9'
        },
        {
            day: 'Thursday: ',
            visitors: '6'
        },
        {
            day: 'Friday: ',
            visitors: '16'
        },
        {
            day: 'Saturday: ',
            visitors: '20'
        }
    ];


    var maxNumberVisitor = Math.max.apply(Math, popularDays.map(function(o) {
        return o.visitors;
    }));
    document.write('Highest visitor count = ' + maxNumberVisitor);
    let objPopularDay = popularDays.find(o => o.visitors === maxNumberVisitor.toString());
    document.write(' on ' + objPopularDay.day + '<br/>');

}


Output: Highest visitor count = 25 on Sunday:

Working Example: Search Array[^]
 
Share this answer
 
Comments
Commish13 16-Jan-18 22:12pm    
Thank you, that works perfectly
Bryian Tan 16-Jan-18 22:49pm    
Awesome. Don't forget to click on the "Accept Solution" button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900