当前位置:网站首页>牛客刷题:数组排序
牛客刷题:数组排序
2022-08-02 21:27:00 【废物的自我修养记录】
题目描述
请补全JavaScript代码,根据预设代码中的数组,实现以下功能:
- 列表只展示数组中的name属性
- 实现点击"销量升序"按钮,列表内容按照销量升序重新渲染
- 实现点击"销量降序"按钮,列表内容按照销量降序重新渲染
注意: - 必须使用DOM0级标准事件(onclick)
实现:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<button class='up'>销量升序</button>
<button class='down'>销量降序</button>
<ul></ul>
<script> var cups = [ {
type: 1, price: 100, color: 'black', sales: 3000, name: '牛客logo马克杯' }, {
type: 2, price: 40, color: 'blue', sales: 1000, name: '无盖星空杯' }, {
type: 4, price: 60, color: 'green', sales: 200, name: '老式茶杯' }, {
type: 3, price: 50, color: 'green', sales: 600, name: '欧式印花杯' } ] var ul = document.querySelector('ul'); var upbtn = document.querySelector('.up'); var downbtn = document.querySelector('.down'); // 补全代码 cups.forEach(ele=>{
const li=document.createElement('li') li.innerText=ele.name; ul.appendChild(li) }) upbtn.onclick=function(){
const lis=document.querySelectorAll('li') Array.from(lis).forEach(ele=>ul.removeChild(ele)) cups.sort((x,y)=>x.sales-y.sales) cups.forEach(ele=>{
const li=document.createElement('li') li.innerText=ele.name; ul.appendChild(li) }) } downbtn.onclick=function(){
const lis=document.querySelectorAll('li') Array.from(lis).forEach(ele=>ul.removeChild(ele)) cups.sort((x,y)=>y.sales-x.sales) cups.forEach(ele=>{
const li=document.createElement('li') li.innerText=ele.name; ul.appendChild(li) }) } </script>
</body>
</html>
边栏推荐
- 【3D视觉】realsense D435三维重建
- 抽象工厂模式
- js function anti-shake and function throttling and other usage scenarios
- 【模型压缩】实例分析量化原理
- Flink-SQL
- Bee 事务注解 @Tran 使用实例
- golang刷letcode:公平分发饼干
- Add and delete all these years, finally planted in MySQL architecture design!
- golang刷leetcode: 卖木头块
- Intensive reading of the Swin Transformer paper and analysis of its model structure
猜你喜欢

圆锥折射作为偏振计量工具的模拟

面试了个985毕业的,回答“性能调优”题时表情令我毕生难忘

Interviewer: can you talk about optimistic locking and pessimistic locks

最近火爆朋友圈的“广告电商”,核心商业模式是什么,广告收入真实靠谱吗?

30天啃透这份Framework 源码手册直接面进大厂

增删改查这么多年,最后栽在MySQL的架构设计上!

测试ESP32-Zigbee转发命令 : 滑轨、继电器控制

2022-08-02 第六小组 瞒春 学习笔记

典型相关分析CCA计算过程

Finally understand: With threads, why do we need coroutines?
随机推荐
JMeter的基本使用
What is the core business model of the "advertising e-commerce" that has recently become popular in the circle of friends, and is the advertising revenue really reliable?
矩阵白化原理及推导
源码构建LAMP环境-3
hi!Don't look at how to SAO gas dye-in-the-wood in MySQL?
Add and delete all these years, finally planted in MySQL architecture design!
YAML文件格式
golang刷leetcode:最大波动的子字符串
测试ESP32-Zigbee转发命令 : 滑轨、继电器控制
C# Monitor class
Command line startup FAQs and solutions
IP协议(网际协议)
行业 SaaS 微服务稳定性保障实战
包管理工具Chocolate - Win10如何安装Chocolate工具、快速上手、进阶用法
双轴晶体中锥形折射的建模与应用
Interviewer: can you talk about optimistic locking and pessimistic locks
命令行启动常见问题及解决方案
golang刷leetcode:到达角落需要移除障碍物的最小数目
sre成长之路
《分布式微服务电商》专题(一)-项目简介