javascript 根据值拆分数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16018407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:57:43  来源:igfitidea点击:

Splitting array based on values

javascriptarraysjsonsplit

提问by Jonas

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

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

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:

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

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.

更新对不起,我没有在最初的问题中发布我的代码,因为我写的东西太垃圾了(我很惭愧)。由于 Bangerang 的解决方案,我最终使用了这里的代码。感谢大家的帮助和贡献。

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

回答by bangerang

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

回答by georg

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

如果您想要更多,请发布您的实际代码;)

回答by Brugnar

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}$

目前,这项工作并不复杂。您只需要遍历您的数组,并在遇到日期格式的单元格时创建一个新数组。在此之前,您使用非日期遇到的值填充先前创建的数组。我想所有的数组内容都是字符串,所以我们将在这里使用一个简单的正则表达式来确定日期格式^[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.

最好的祝福。