码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 已知 list 数组请写出一段代码,实现功能: 数组内type 相同的元素只保留 votes 最大的元素,并且元素需要按照 votes 从大到小排序。


    题目

    1. var list = [{type:1, des:"dddd", votes:80},
    2. {type:2, des:"dddd", votes:60},
    3. {type:3, des:"dddd", votes:70},
    4. {type:1, des:"dddd", votes:50},
    5. {type:3, des:"dddd", votes:80},
    6. {type:8, des:"dddd", votes:80},
    7. {type:9, des:"dddd", votes:20},
    8. {type:1, des:"dddd", votes:100},
    9. {type:3, des:"dddd", votes:30}]

    分析

    读题:首先第一步需要将type等于1的挑选出来,只保留最大的元素,

     然后需要将数组以votes从大到小排列

    代码

    1. var list = [
    2. { type: 1, des: "dddd", votes: 80 },
    3. { type: 2, des: "dddd", votes: 60 },
    4. { type: 3, des: "dddd", votes: 70 },
    5. { type: 1, des: "dddd", votes: 50 },
    6. { type: 3, des: "dddd", votes: 80 },
    7. { type: 8, des: "dddd", votes: 80 },
    8. { type: 9, des: "dddd", votes: 20 },
    9. { type: 1, des: "dddd", votes: 100 },
    10. { type: 3, des: "dddd", votes: 30 },
    11. ];
    12. // 使用 reduce 方法合并相同 type 的元素,并保留 votes 最大的那个
    13. var result = list.reduce((accumulator, currentItem) => {
    14. // 查找 accumulator 中是否已经有一个相同 type 的元素
    15. var existingItem = accumulator.find(
    16. (item) => item.type === currentItem.type
    17. );
    18. // 如果没有找到,或者找到的元素的 votes 比当前元素的 votes 小,则添加或替换当前元素
    19. if (!existingItem || existingItem.votes < currentItem.votes) {
    20. if (!existingItem) {
    21. accumulator.push(currentItem);
    22. } else {
    23. existingItem.votes = currentItem.votes;
    24. // 如果你想完全替换 existingItem,你可以这样做:
    25. // accumulator[accumulator.indexOf(existingItem)] = currentItem;
    26. }
    27. }
    28. return accumulator;
    29. }, []);
    30. // 使用 sort 方法对结果按照 votes 从大到小排序
    31. result.sort((a, b) => b.votes - a.votes);
    32. console.log(result);

    效果图

    结语

    点个赞啦!

  • 相关阅读:
    ChatGPT支持下的PyTorch机器学习与深度学习技术应用
    Angular 里的 Service Worker
    微信小程序的高校教室自习室占座预约系统java+uniapp
    [附源码]java毕业设计鑫地酒店酒水库存管理系统论文
    flutter课程(The Complete 2021 Flutter Development Bootcamp with Dart)学习总结
    学校档案管理系统软件-学校数字档案室解决方案
    echarts仪表盘vue
    【Jmeter】在进行综合场景压测时,由于不同的请求,要求所占比例不同,那如何实现呢?
    Bit, byte, KB, GB, MG
    安卓:解决AndroidStudio导出Unity的Apk(APP)出现2个显示图标
  • 原文地址:https://blog.csdn.net/GAGGAAAAA/article/details/136386471
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号