在数组中互换一些值并将其推到不同的索引

发布于 2025-02-09 06:01:00 字数 1031 浏览 1 评论 0原文

例如:

我有一个数组列表,其值为:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晰传感 2025-02-16 06:01:00

您可以通过逗号(以及之后的可选空间)将每个值拆分,然后使用降低将第二部分移动以成为下一个索引的第一部分。如果在该循环的末尾仍有剩余,则以第一个数组值插入:

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:58", 
    "10-45-23:59, 00:00-04:59",
    "10-45:22:45"
];

let result = data.map(s => s.split(/,\s*/));
// Only change something when there is an item that does not have doubly entry
if (result.some(({length}) => length < 2)) { // must rotate second items
    let overflow;
    [result, overflow] = result.reduce(([acc, prev], parts) =>
        [[...acc, [...prev, parts.shift()].join(",")], parts]
    , [[], []]);
    // What drops off at the end, should be inserted at the start
    result[0] = [...overflow, result[0]].join(",");
}
console.log(result);

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:

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:58", 
    "10-45-23:59, 00:00-04:59",
    "10-45:22:45"
];

let result = data.map(s => s.split(/,\s*/));
// Only change something when there is an item that does not have doubly entry
if (result.some(({length}) => length < 2)) { // must rotate second items
    let overflow;
    [result, overflow] = result.reduce(([acc, prev], parts) =>
        [[...acc, [...prev, parts.shift()].join(",")], parts]
    , [[], []]);
    // What drops off at the end, should be inserted at the start
    result[0] = [...overflow, result[0]].join(",");
}
console.log(result);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文