Javascript显示数组的值 [英] Javascript display value of an array

查看:133
本文介绍了Javascript显示数组的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,我显示'访客'的最高整数值。我还想显示与访客的最高整数相关联的天的值。现在我的代码显示正确的访问者值,但显示天的所有值。以下是我的尝试。感谢您的帮助。



我的尝试:



< pre lang =Javascript><!doctype html>

< meta charset = utf-8>
< title>天

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:' 星期日:',访客:' 25'},
{day:' 星期一:',访客:' 13'},
{day:' 星期二:',访客:' 3'},
{day:' 星期三:',访客:' 9'},
{day:' 星期四:',访客:' 6'},
{day:' 星期五:',访客:' 16'} ,
{天:' 星期六:',访客:' 20'}
];


var weekDay = popularDays.map( function (o ){
return o.day;
});

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

var maxValue = 数学 .max.apply(数学,最大值);

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

var value = weekDay.valueOf(maxValue);

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





 <   p     id   =  lblmostPopularDays >  lblmostPopularDays <   / p  >  

解决方案

您可以尝试以下内容。首先找到最大访客数,然后根据计数搜索一天。



 mostPopularDays( 10 ); 

function mostPopularDays(week){
var popularDays = [ {
天:' 星期日:'
访客:' 25'
},
{
day:' 星期一:'
访客:' 13'
},
{
day:' 星期二:'
访客:' 3'
},
{
day:' Wednesday:'
访问者:' 9'
},
{
day:' 星期四:'
访客:' 6'
},
{
day:' Friday:'
访客:' 16'
},
{
天:' 星期六:'
访问者:' 20'
}
];


var maxNumberVisitor = 数学 .max.apply(< span class =code-sdkkeyword> Math
,popularDays.map( function (o){
返回 o.visitors;
}));
document .write(' 最多访客数量=' + maxNumberVisitor);
objPopularDay = popularDays.find(o => o.visitors === maxNumberVisitor.toString());
document .write(' on' + objPopularDay.day + ' < br />');

}





输出:周日最高访客人数= 25:



工作示例:搜索阵列 [ ^ ]


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:

<!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);				
}



<p id="lblmostPopularDays">lblmostPopularDays</p>

解决方案

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

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[^]


这篇关于Javascript显示数组的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆