最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Splitting array based on values - Stack Overflow

programmeradmin0浏览0评论

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
)

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.

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

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
)

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.

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("},]","}]"); 
Share Improve this question edited Apr 16, 2013 at 15:00 Jonas asked Apr 15, 2013 at 14:56 JonasJonas 3231 gold badge4 silver badges15 bronze badges 4
  • /^(\d{4})-(\d{2})-(\d{2})$/ does not match the date format in your example. Are the dates stored as String or Numbers? – Kijewski Commented Apr 16, 2013 at 14:57
  • Sorry yes, you are right, I did change the date format in the meantime (discovered RegExp and exec()). I will update the question to reflect my change. – Jonas Commented Apr 16, 2013 at 15:00
  • Please do not put "solved" in the title. Stackoverflow color-codes questions with an accepted answer. – Kijewski Commented Apr 16, 2013 at 15:02
  • Today, reduce could be used instead: Array splitting based on value. – Sebastian Simon Commented Nov 24, 2020 at 7:48
Add a comment  | 

3 Answers 3

Reset to default 7

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

basically,

result = []

sourceArray.forEach(function(element) {
    if(element looks like a date)
        result.push([])
    result[result.length - 1].push(element)
})

post your actual code if you want more ;)

Currently, the work is not really complicated. You just have to go through your array, and create a new one when you encounter a date formatted cell. Until then, you fill the previous created array with the non-date encountered values. I suppose that all of the array content are string, so we will determine here the date format using a simple regex ^[0-9]{8}$

var result = new Array();
var date_format = /^[0-9]{8}$/;

for (var k in big_array) {
    // if date format, add a new array
    if (date_format.test(result[k])) {
        result.push(new Array());
    }
    // finally, push the value in the last array
    result[result.length-1].push(big_array[k]);
}

This is a really simple algorithm exercise, so i would recommend to practice more the code's writing and avoid asking next time, or at least show what you tried.

Best regards.

发布评论

评论列表(0)

  1. 暂无评论