gpt4 book ai didi

javascript - 根据值拆分数组

转载 作者:搜寻专家 更新时间:2023-11-01 05:06:00 24 4
gpt4 key购买 nike

我已经有很长时间没有编写一些 javascript 代码了,而且我很生疏。

我需要像这样拆分一个数组:

Object (    [0] => 2013-04-12    [1] => text    [2] => text    [3] => text    [4] => text    [5] => 2013-04-11    [6] => text    [7] => text    [8] => text    [9] => text    [10] => text    [11] => text    [12] => 2013-04-10    [13] => text    [14] => text    [15] => text    [16] => text    [17] => 2013-04-09    [18] => text    [19] => text    [20] => text)

into json objects or arrays where index 0 is always the date:

Object0 (    [0] => 2013-04-12    [1] => text    [2] => text    [3] => text    [4] => text)Object1 (    [0] => 2013-04-11    [1] => text    [2] => text    [3] => text    [4] => text    [5] => text    [6] => text)Object2 (    [0] => 2013-04-10    [1] => text    [2] => text    [3] => text    [4] => text)Object3 (    [0] => 2013-04-09    [1] => text    [2] => text    [3] => text)

So as you can see, I can only use the value to figure out each new array, which can be of different sizes.

Thanks for any help.

UPDATESorry I didn't publish my code in the initial question because what I had written was way too crap (and I was very ashamed).Here the code I used in the end thanks to the solution from Bangerang.Thanks everyone for your help and contributions.

var events = [];
var result = [];
var j = 0;
for (var i = 0; i < tableData.length; i++){
if (/^(\d{4})-(\d{2})-(\d{2})$/.test(tableData[i])){ //Returning true if it's just numbers
j = j + 1;
events[j] = [];
events[j].push(tableData[i]);
result.push(events[j]);
} else {
events[j].push(tableData[i]);
}
}

// Then I write the json message which will be eval to use with FullCalendar.
var json_events = '[';
for (var i = 1; i <= events.length; i++){
if (typeof events[i] !== 'undefined' && events[i].length > 0) {
for (var j = 0; j < events[i].length; j++){
if(/@/.test(events[i][j])){

json_events += '{"start":"' + events[i][0] + '",';
json_events += '"textColor":"black",';

var tmp = events[i][j].split("@");
json_events += '"backgroundColor":"' + tmp[0] + '",';

var text_pattern = new RegExp(/<b>([a-z_]+)<\/b>(.+)/);
var title_desc = text_pattern.exec(tmp[1]);
json_events += '"title":"' + title_desc[1] + '",';
json_events += '"description":"' + title_desc[0] + '"';
json_events += '},';
}
}
}
}
json_events += ']';
json_events = json_events.replace("},]","}]");

最佳答案

假设日期是唯一包含数字的日期,您可以试试这个:

var object = [];
var result = [];
for (var i = 0; i < objects.length; i++){
if (/^\d+$/.test(objects[i])){ //Returning true if it's just numbers
object = []
object.push(objects[i]);
result.push(object);
}else {
object.push(objects[i]);
}
}

关于javascript - 根据值拆分数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16018407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com