1

我有这样的字符串

var data1 = "Cust_Id","Name","Mobile","Email","Last_time_Interface"
"1","Aruna","9980600155","Aruna@gmail.com","2013-01-21"
"1","Aruna","9980600155","Aruna@gmail.com","2013-02-21"
"2","Sam","9980600255","Sam@gmail.com","2009-09-28";

但我需要将其转换为如下所示

var data = [["Cust_Id","Name","Mobile","Email","Last_time_Interface"],
["1","Aruna","9980600155","Aruna@gmail.com","2013-01-21"],
["1","Aruna","9980600155","Aruna@gmail.com","2013-02-21"],
["2","Sam","9980600255","Sam@gmail.com","2009-09-28"]];
4

1 回答 1

0

假设您有一个数组中的字符串列表,并且您希望将其拆分为 5 个元素的块:

var data1 = ["Cust_Id","Name","Mobile","Email","Last_time_Interface",
    "1","Aruna","9980600155","Aruna@gmail.com","2013-01-21",
    "1","Aruna","9980600155","Aruna@gmail.com","2013-02-21",
    "2","Sam","9980600255","Sam@gmail.com","2009-09-28"],
    data;

_.mixin({
    chunk : function (array, unit) {
        if (!_.isArray(array)) return array;
        unit = Math.abs(unit);
        var results = [],
        length = Math.ceil(array.length / unit);

        for (var i = 0; i < length; i++) {
            results.push(array.slice( i * unit, (i + 1) * unit));
        }
        return results;
    }
});
data = _.chunk(data1, 5);
console.clear();
console.log(data);

underscore.js扩展chunk最初是由 Larry Battle 编写的。这是一个测试页

于 2013-09-17T09:32:45.977 回答