当前位置:网站首页>牛客刷题:数组排序
牛客刷题:数组排序
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>
边栏推荐
猜你喜欢
IP Protocol (Internet Protocol)
FRED应用:激光二极管光源耦合到光纤的仿真
The interviewer asked me: delete library, in addition to run do?
Swin Transformer 论文精读,并解析其模型结构
matplotlib绘图的核心原理讲解(超详细)
二叉搜索树的实现
你我都会遇到的需求:如何导出MySQL中的数据~ 简单!实用!
若依如何实现添加水印功能
PyRosetta 安装方法之Conda安装
How to seize the new trend of NFT, yuan|universe|universe?
随机推荐
什么是幂等
golang 刷leetcode:从栈中取出 K 个硬币的最大面值和
【C语言进阶】--指针典题剖析
golang刷letcode:公司命名
手把手教你干掉if else
你我都会遇到的需求:如何导出MySQL中的数据~ 简单!实用!
Redis是如何轻松实现系统秒杀的?
golang刷leetcode:按位与结果大于零的最长组合
Summary of @Transactional transaction invocation and effective scenarios
汇编语言中b和bl关键字的区别
Byte's internal technical map is amazing and practical
SRv6网络演进面临的挑战
[Dry goods] Best practice of sub-library and sub-table
golang刷letcode:公平分发饼干
《分布式微服务电商》专题(一)-项目简介
博客主题美化第二弹
ML之PDP:基于titanic泰坦尼克是否获救二分类预测数据集利用PDP部分依赖图对RF随机森林和LightGBM模型实现可解释性案例
JS函数防抖&函数节流及其使用场景
You and I will meet the needs of: how to export the data in a MySQL simple ~!Practical!
Finally understand: With threads, why do we need coroutines?