当前位置:网站首页>Object.keys后key值数组乱序的问题
Object.keys后key值数组乱序的问题
2022-04-23 12:39:00 【AzeShinja】
关于造成这个问题的原因
为什么Object.keys等遍历对象keys值会造成乱序输出的问题
我的需求:
每个试题名字后有相关的单元ID,我是根据截取字符串做的排序规则,这会使单元1正常显示在第一个的位置
如果说没有相关规则,兄弟可以自己想办法造一个,或者和后端沟通好,显示的顺序是什么,因为毕竟想要有顺序展示的需求就一定是有相关规则的
我给出的这个问题的其中之一的解决方案如下:
我的处理之前的数据类型
看起来这里没有什么问题,但是通过Object.keys输出后 是单元2 在单元1的前面,所以这里我们需要对处理后的数据进行排序
const stringToArrSort = (target) => {
return Number(target.slice(target.lastIndexOf('-') + 1));
};
const sortArrByUnitId = (listData) => {
return listData.sort((a, b) => {
const sortA = stringToArrSort(a);
const sortB = stringToArrSort(b);
return sortA - sortB;
});
};
我是通过 ‘-’ 把id取出来,进行比大小,这样输出出来的结果就能正常按顺序展示了
版权声明
本文为[AzeShinja]所创,转载请带上原文链接,感谢
https://blog.csdn.net/AzeShinja/article/details/124272204
边栏推荐
- [daily question] chessboard question
- What are the forms of attack and tampering on the home page of the website
- MySQL函数-递归函数
- Qt重绘事件与剪切
- BUUCTF WEB [BJDCTF2020]The mystery of ip
- AD20补充笔记3—快捷键+持续更新
- 基于卷积神经网络的遥感影像分类识别系统
- BUUCTF WEB [GXYCTF2019]禁止套娃
- BUUCTF WEB [BUUCTF 2018]Online Tool
- [redis series] redis learning 13. Redis often asks simple interview questions
猜你喜欢
解决disagrees about version of symbol device_create
如何防止网站被黑客入侵篡改
QT double buffer drawing
SSL证书退款说明
Number of nodes of complete binary tree
Everything can be expected in the future | one 2022 campus recruitment officially opened
Idea setting copyright information
解锁OpenHarmony技术日!年度盛会,即将揭幕!
标签与路径
Introduction to metalama 4 Use fabric to manipulate items or namespaces
随机推荐
How to switch PHP version in Windows 2008 system
Uni app native app cloud packaging integrated Aurora push (jg-jpush) detailed tutorial
AD20补充笔记3—快捷键+持续更新
MySQL function - recursive function
Jiachen chapter Genesis "inner universe" joint Edition
QT draw image
Fashion cloud learning - input attribute summary
QT interprocess communication
Qt双缓冲绘图
NBIOT的AT指令
网络信息安全之零信任
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
Stacks and queues a
[wechat applet] Z-index is invalid
Zigbee之CC2530最小系统及寄存器配置(1)
洛谷P5540 [BalkanOI2011] timeismoney | 最小乘积生成树 题解
0基础可以考CPDA数据分析师证书吗
免费试用一个月的服务器,并附上教程
【Redis 系列】redis 学习十三,Redis 常问简单面试题
SQL exercise (I)