当前位置:网站首页>The roll call system and array elements find maximum and minimum values for sorting of objects
The roll call system and array elements find maximum and minimum values for sorting of objects
2022-08-02 03:59:00 【yorup】
I. Roll call system
1. Write the basic elements first, here is a brief write up
???The initial effect with no styles added is as follows:

2. In order to look better, add some CSS styles below
.box{width: 200px;margin: auto;margin-top: 20px;}.box div{background: red;color: #fff;width: 100%;height: 80px;text-align: center;line-height: 80px;font-size: 18px;}.box button{width: 100%;height: 35px;text-align: center;margin-top: 10px;border: 0px;background: blue;color: #fff;}The effect after adding is as follows

3. Next, click "Start" to realize the roll call system. The setinterval() timer is mainly used to realize the effect of automatic click, and the "start" and "pause" of the button are realized by marking true and false with flag.
var realnames = ["Zhang San", "Li Si", "Zhao Liu", "Wang Wu", "Lu Qi", "Wang Erma", "Dogleft"];var realname = document.getElementById("realname");var flag = false;var btn = document.getElementById("btn");function call(){flag = !flag;if(flag){window.myInter = setInterval(function(){var num = Math.floor(Math.random()*realnames.length);realname.innerHTML = realnames[num];},100);btn.innerText = "Pause";}else{clearInterval(window.myInter);//Clear the timerbtn.innerText = "Start";}}The final effect is as follows

Second, the array elements are the elements of the object sorting to find the maximum and minimum age (two methods)
The arrays that need to find the maximum and minimum values are as follows
var data = [{realname:"Zhang San",age:19},{realname:"Wang 5 1",age:128},{realname:"Wang Wu 2",age:128},{realname:"Li Si",age:20},{realname:"Wang Wu",age:28},{realname:"Zhao Liu",age:19},];Method 1: Use the sort() method
data.sort(function(a,b){return a.age - b.age;});console.log(data);var min = data[0];var max = data[data.length-1];The results are as follows

At this point, you can see that there are multiple maximum and minimum values, and you need to output them all
for(var i=0;iThe effect is as follows

Method 2: Use a for loop to sort.Assuming that the age value of the first element is the largest (minimum), traverse each element in the array, and replace the initial value when there is an age value greater than the initial value.
var arrmax = [];var arrmin = [];var max = data[0];var min = data[0];for(var i=0; idata[i].age) {min = data[i];}}//Same as method 1 to get multiple maximum and minimum valuesfor(var i=0;i Note: If you have any questions, please leave a comment
边栏推荐
- CTF入门笔记之ping
- easyswoole uses redis to perform geoRadiusByMember Count invalid fix
- (1)Thinkphp6入门、安装视图、模板渲染、变量赋值
- [campo/random-user-agent]随机伪造你的User-Agent
- hackmyvm: controller walkthrough
- QR code generation API interface, which can be directly connected as an A tag
- Function hoisting and variable hoisting
- Multithreading (implementing multithreading, thread synchronization, producer and consumer)
- Introduction to PHP (self-study notes)
- ES6三点运算符、数组方法、字符串扩展方法
猜你喜欢

IO流、 编码表、 字符流、 字符缓冲流

PHP8.2中字符串变量解析的新用法

DVWA drone installation tutorial

web渗透必玩的靶场——DVWA靶场 1(centos8.2+phpstudy安装环境)

(5) 模块与包、编码格式、文件操作、目录操作

Alfa: 1 vulnhub walkthrough

阿里云设置域名解析重定向后,无法使用Chrome访问

(7) 浅学 “爬虫” 过程 (概念+练习)

IO stream, encoding table, character stream, character buffer stream

(3) Thinkphp6 database
随机推荐
SQL:DDL、DML、DQL、DCL相应介绍以及演示
PHP8.2 version release administrator and release plan
Kali环境下Frida编写脚本智能提示
What will be new in PHP8.2?
(4) Function, Bug, Class and Object, Encapsulation, Inheritance, Polymorphism, Copy
Add a full image watermark to an image in PHP
(8) requests, os, sys, re, _thread
PHP基金会三月新闻公告发布
VIKINGS: 1 vulnhub walkthrough
SQL: DDL, DML, DQL, DCL corresponding introduction and demonstration
Query the indexes of all tables in the database and parse them into sql
14. JS Statements and Comments, Variables and Data Types
Praying: 1 vulnhub walkthrough
IO stream, encoding table, character stream, character buffer stream
1.初识PHP
SQL分类、DQL(数据查询语言)、以及相应SQL查询语句演示
宝塔邮局邮箱设置成功后能发送不能接收问题处理
PHP8.2将会有哪些新东西?
[league/climate]一个功能健全的命令行功能操作库
PHP图片压缩到指定的大小