在数组中互换一些值并将其推到不同的索引
例如:
我有一个数组列表,其值为:
let data = [
"10-45-23:45",
"10-45-22:45",
"10-45-20:45",
"10-45-23:45",
"10-45-23:59,00:00-04:59",
"10-45-23:59, 00:00-04:59",
"10-45:22:45"
]
因此,我要求解的内容是在索引中填充索引之后的任何东西,即00:00-04:59需要向下推一个索引。因此,可以以适当的格式对数据进行格式。 我有任何动态的方式如何解决这个问题?
我所需的输出:
let data = [
"10-45-23:45",
"10-45-22:45",
"10-45-20:45",
"10-45-23:45",
"10-45-23:59",
" 00:00-04:59,10-45-23:59",
"00:00-04:59,10-45:22:45"
];
然后将数据分类为:
let data = [
"10-45-23:45",
"10-45-22:45",
"10-45-20:45",
"10-45-23:45",
"10-45-23:59",
"10-45-23:59,00:00-04:59",
"10-45:22:45,00:00-04:59"
];
但是,如果数据数组已经以低于低的格式进行,那么我们不会更改数组:
let data = [
"10-45-23:59,00:00-01:59",
"10-45:22:59,00:00-02:59",
"10-45-23:59,00:00-04:59",
"10-45:22:59,00:00-04:59",
"10-45-23:59,00:00-04:59",
"10-45:22:59,00:00-04:59",
"10-45-23:59,00:00-04:59",
"10-45:22:59,00:00-04:59"
]
For an example:
I have a list of array which holds value as:
let data = [
"10-45-23:45",
"10-45-22:45",
"10-45-20:45",
"10-45-23:45",
"10-45-23:59,00:00-04:59",
"10-45-23:59, 00:00-04:59",
"10-45:22:45"
]
So, what I am trying to solve is anything that's after a comma inside an index i.e. the 00:00-04:59 needs to be pushed one index downwards. So, that the data can be formatted in the proper format.
I there any dynamic way how we could fix this?
My Desired Output:
let data = [
"10-45-23:45",
"10-45-22:45",
"10-45-20:45",
"10-45-23:45",
"10-45-23:59",
" 00:00-04:59,10-45-23:59",
"00:00-04:59,10-45:22:45"
];
then sort the data to:
let data = [
"10-45-23:45",
"10-45-22:45",
"10-45-20:45",
"10-45-23:45",
"10-45-23:59",
"10-45-23:59,00:00-04:59",
"10-45:22:45,00:00-04:59"
];
However, If the data array is already in the below-mentioned format then we do no changes to the array:
let data = [
"10-45-23:59,00:00-01:59",
"10-45:22:59,00:00-02:59",
"10-45-23:59,00:00-04:59",
"10-45:22:59,00:00-04:59",
"10-45-23:59,00:00-04:59",
"10-45:22:59,00:00-04:59",
"10-45-23:59,00:00-04:59",
"10-45:22:59,00:00-04:59"
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过逗号(以及之后的可选空间)将每个值拆分,然后使用
降低
将第二部分移动以成为下一个索引的第一部分。如果在该循环的末尾仍有剩余,则以第一个数组值插入:You can split each value by comma (and optional white space after it), and then use
reduce
to shift those second parts to become the first parts at the next index. If at the end of that loop there is still a remainder, it is inserted at the first array value: