当前位置:网站首页>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
边栏推荐
- Fastjson 2 is coming, the performance continues to improve, and it can fight for another ten years
- 没有空闲服务器?导入 OVF 镜像快速体验 SmartX 超融合社区版
- Xinwangda announced that the price of battery products had been increased, and the investment of "weixiaoli" exceeded 1 billion
- What are the forms of attack and tampering on the home page of the website
- MySQL function - recursive function
- MySQL函数-递归函数
- 智能多线弹性云增加独立的IP地址,如何实现多线功能?
- NPDP|产品经理如何做到不会被程序员排斥?
- Lesson 26 static member functions of classes
- STM32CubeProgrammer基础使用说明
猜你喜欢
![[daily question] chessboard question](/img/70/54a924d07c935965e54b96d9b07278.png)
[daily question] chessboard question

CGC: contractual graph clustering for community detection and tracking

标签与路径
![leetcode:437. Path sum III [DFS selected or not selected?]](/img/e7/d0983a3251dfaf2cf33b56a12ac149.png)
leetcode:437. Path sum III [DFS selected or not selected?]

Qt绘制图像

A graphic designer's fantasy world | ones characters

leetcode:437. 路径总和 III【dfs 选还是不选?】

QT draw image

Qt进程间通信

Metalama简介4.使用Fabric操作项目或命名空间
随机推荐
QT draw text
Introduction to metalama 4 Use fabric to manipulate items or namespaces
A graphic designer's fantasy world | ones characters
31岁才转行程序员,目前34了,我来说说我的经历和一些感受吧...
How does sqlserver insert or update the number of weeks of the day instead of text
Analysis of InnoDB execution process in MySQL
MySQL function - recursive function
在 VSCode 中调试 Jest 的测试用例,VSCode调试Jest测试用例报错basedir=$(dirname “$(echo “$0“ | sed -e ‘s,\\,/,g‘)“)解决
关于使用Go语言创建WebSocket服务浅谈
为什么hash%length==hash&(length-1)的前提是 length 是 2 的 n 次方
Symmetric encryption, certificate encryption
C set Logo Icon and shortcut icon
Idea setting copyright information
The maximum number of remote desktop servers has been exceeded
Hard core parsing promise object (do you know these seven common APIs and seven key questions?)
BUUCTF WEB [BUUCTF 2018]Online Tool
leetcode:437. Path sum III [DFS selected or not selected?]
SPSS之单因素方差分析
洛谷P5540 [BalkanOI2011] timeismoney | 最小乘积生成树 题解
Image attribute of input: type attribute of fashion cloud learning -h5