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 に答える