当前位置:网站首页>Get the multi-functional version of the maximum and minimum values of the internal values of the objects in the array and the full version of the roll call system, and show the effect
Get the multi-functional version of the maximum and minimum values of the internal values of the objects in the array and the full version of the roll call system, and show the effect
2022-07-19 05:12:00 【Create splendid -- hand in hand with you】
1, Get the maximum and minimum values of the internal values of the objects in the array
Multi function code :
data.sort(function(a,b){
return b.age-a.age;
})
console.log(data);
analysis : The multifunctional part of this part of the code is , It can effectively solve the size arrangement and the arrangement from small to large or from large to small , At the same time, it can also display the same column , It is helpful to get the numerical elements in the same part below , Methods commonly used in work ;
The case analysis is as follows :
Through the following analysis , You can better understand the above multi-function code and various calculations, and you can get the method to obtain the maximum and minimum values inside the object in the array ;
Case study :
Output the information of the largest student in the following array , And the youngest student information .
example :var data= [
{realname:" Zhang San ",age:19},
{realname:" Li Si ",age:20},
{realname:" Wang Wu ",age:28},
{realname:" Zhang San ",age:19},
{realname:" Wang Wu ",age:28},
{realname:" Zhao Liu ",age:21},
]
answer : The first method :
var temp;
var max=data[0];
var min=data[0];
// For maximum
// for(var i=0;i<data.length;i++){
// if(max.age<data[i].age){
// max=data[i];
// }
// }
// console.log(max);
// minimum value
// for(var i=0;i<data.length;i++){
// if(min.age>data[i].age){
// min=data[i];
// }
// }
// console.log(min);
The second solution :
// data.sort(function(a,b){
// return b.age-a.age;
// })
console.log(data);
If there are values of the same part, then there are the following parts , You can get the common value of parts of the same size :
// Storage variable
var maxs = [];
var mins = [];
data.sort(function(a, b) {
return b.age - a.age;
})
// What is defined here is to store the first value and the last value
min = data[0];
max = data[data.length - 1]
// Below for Loop is to control the same arrangement of two values and add them to the end
for (var i = 0; i < data.length; i++) {
if (max.age == data[i].age) {
maxs.push(data[i]);
}
if (min.age == data[i].age) {
mins.push(data[i]);
}
}
console.log(maxs, mins);
2, Full version of roll call system :
css part :
#text {
width: 50%;
height: 500px;
margin: 200px auto;
background-color: orangered;
}
.box {
width: 200px;
height: 30px;
text-align: center;
margin-left: 42%;
background-color: #fff;
}
.bot {
margin-left: 50%;
}HTML The content part :
<!-- <div id="text">
<div class="box"></div>
<button class="bot"> Start </button>
</div> -->js The content part :
// var arr=[' Zhang San ',' Li Si ',' Wang Wu ',' Zhang Liang ',' Li Liangmei '];
// var temp=document.querySelector('.box');
// var bot=document.querySelector('.bot');
// var flag=false;
// bot.addEventListener('click',function(){
// flag=!flag;
// var top=setInterval(function(){
// if(flag){
// function getRandom(min,max){ // Select the formula with the piece
// return Math.floor(Math.random()*(max-min))+min;
// }
// temp.innerHTML=arr[getRandom(0,5)];
// bot.innerHTML=' Pause ';
// }else{
// clearInterval(top);
// bot.innerHTML=' Start ';
// }
// },50)
// })Final effect :

边栏推荐
猜你喜欢

Harmonyos third training notes

IText modify PDF Text

第十届泰迪杯数据挖掘挑战赛A题害虫识别YOLOv5模型代码(已跑通,原创作品,持续更新)

小程序云开发表单提交并在页面中获取数据

关于New_Online_Judge_1081_哥德巴赫猜想的思考

Actual cases of data analysis and data mining local house price prediction (716):

Uni app conditional compilation ifdef ENDIF compatible with multiple terminals

es6新增-运算符的扩展

使用Echars实现水滴状、环形图、分割图、堆叠、组织架构图、地图轮廓等图表

数据可视化
随机推荐
数据分析与数据挖掘实战案例本地房价预测(716):
C语言练习题
Class object automatic injection attribute operation tool
Mysql database experiment training 6, data view (detailed)
IDL 读取葵花8(Himawari-8)HSD数据
es6新增-数组/对象的解构赋值
【C语言—零基础第七课】顺序结构与选择结构
6S参数
实习项目1-个性化主页配置
Infinite classification
uniapp 表单(input、radio、picker)提交获取参数值
The code of yolov5 model for pest identification in Title A of the 10th Teddy cup data mining challenge (has been run through, original works, continuously updated)
02_ Movie recommendation (contentbased)_ User portrait
百度地图 实现 热力图
游玩数据获取与数据分析、数据挖掘 【2022.5.30】
微信小程序云开发使用方法-1
【Es6】利用添加数据,筛选并传输至页面等多项功能实现案例
cookie是否有效时间限定?如何设置cookie?手把手教你设置
User login - and create SMS verification code
机器学习之PCA特征降维+案例实践