基于值拆分数组 [英] Splitting array based on values

查看:96
本文介绍了基于值拆分数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我不得不处理一些javascript代码以来,已经很长时间了。我生气了。

It's been a long time since I had to work on some javascript code and I'm a lot rusty.

我需要拆分这样的数组:

I need to split an array like this:


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
)

进入json对象或数组,其中索引0始终是日期:

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.

感谢您的帮助。

更新
抱歉,我没有在最初的问题中发布我的代码,因为我写的东西太废了(我感到非常惭愧)。
这里我使用的代码归功于Bangerang的解决方案。
感谢大家的帮助和贡献。

UPDATE Sorry 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("},]","}]"); 


推荐答案

假设日期是唯一只有数字的日期你可以试试这个:

Assuming that the date's is the only ones with just numbers in it you can try this:

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]);
    }
}

这篇关于基于值拆分数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆