使用underscore.js将javascript数组分割成块

时间:2022-01-07 03:07:36

I need to split a javascript array into n sized chunks.

我需要将一个javascript数组分割成n个大小的块。

E.g.: Given this array

例如:这个数组

["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13"]

and a n equals to 4, the output should be this:

n等于4,输出应该是这样的

[ ["a1", "a2", "a3", "a4"],
  ["a5", "a6", "a7", "a8"],
  ["a9", "a10", "a11", "a12"],
  ["a13"]
]

I aware of pure javascript solutions for this problem, but since I am already using underscore.js I am wondering if underscore provides a better solution for this.

我知道这个问题的纯javascript解决方案,但是因为我已经在使用下划线了。我想知道下划线是否提供了更好的解决方案。

Edit:

I created a jsPerf test to check how much slower the underscore solution is.

我创建了一个jsPerf测试,以检查下划线解决方案的速度有多慢。

5 个解决方案

#1


84  

Take a look at lodash' chunk: https://lodash.com/docs#chunk

看看lodash: https://lodash.com/docs#chunk

var data = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13"];
_.chunk(data, 3); 
// [
//  ["a1", "a2", "a3"],
//  ["a4", "a5", "a6"],
//  ["a7", "a8", "a9"],
//  ["a10", "a11", "a12"],
//  ["a13"]
// ]

#2


89  

For Underscore based solution try this:

对于基于下划线的解决方案,请尝试以下方法:

var data = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13"];
var n = 3;
var lists = _.groupBy(data, function(element, index){
  return Math.floor(index/n);
});
lists = _.toArray(lists); //Added this to convert the returned object to an array.
console.log(lists);

Using the chain wrapper method you can combine the two statements as below:

使用链包装方法,您可以将以下两个语句组合在一起:

var data = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13"];
var n = 3;
var lists = _.chain(data).groupBy(function(element, index){
  return Math.floor(index/n);
}).toArray()
.value();

#3


4  

A possibly simpler expression:

可能简单的表达式:

    _.range(coll.length / n).map(i => coll.slice(i * n, (i + 1) * n))

#4


0  

try this one it is much more practical (for example, if you would want to split the array based on amount of items to be container in each sub array):

试试这个更实用(例如,如果您希望根据每个子数组中要包含的项的数量来分割数组):

function chunk(arr, start, amount){
    var result = [], 
        i, 
        start = start || 0, 
        amount = amount || 500, 
        len = arr.length;

    do {
        //console.log('appending ', start, '-', start + amount, 'of ', len, '.');
        result.push(arr.slice(start, start+amount));
        start += amount;

    } while (start< len);

    return result;
};

and the use in your case:

在你的案例中使用:

var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17],
    chunked = chunk(arr, 0, Math.floor(arr.length/3)); //to get 4 nested arrays

console.log(chunked);

and another case:

和另一个案例:

var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17],
    chunked = chunk(arr, 0, 3); // to get 6 nested arrays each containing maximum of 3 items

console.log(chunked);

#5


0  

Underscore supports _.chunk() natively as of version 1.9.0.

下划线在1.9.0版本中支持_.chunk()。

let data = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13"];
_.chunk(data, 4);
// [
//   ["a1", "a2", "a3", "a4"],
//   ["a5", "a6", "a7", "a8"],
//   ["a9", "a10", "a11", "a12"],
//   ["a13"]
// ]

#1


84  

Take a look at lodash' chunk: https://lodash.com/docs#chunk

看看lodash: https://lodash.com/docs#chunk

var data = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13"];
_.chunk(data, 3); 
// [
//  ["a1", "a2", "a3"],
//  ["a4", "a5", "a6"],
//  ["a7", "a8", "a9"],
//  ["a10", "a11", "a12"],
//  ["a13"]
// ]

#2


89  

For Underscore based solution try this:

对于基于下划线的解决方案,请尝试以下方法:

var data = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13"];
var n = 3;
var lists = _.groupBy(data, function(element, index){
  return Math.floor(index/n);
});
lists = _.toArray(lists); //Added this to convert the returned object to an array.
console.log(lists);

Using the chain wrapper method you can combine the two statements as below:

使用链包装方法,您可以将以下两个语句组合在一起:

var data = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13"];
var n = 3;
var lists = _.chain(data).groupBy(function(element, index){
  return Math.floor(index/n);
}).toArray()
.value();

#3


4  

A possibly simpler expression:

可能简单的表达式:

    _.range(coll.length / n).map(i => coll.slice(i * n, (i + 1) * n))

#4


0  

try this one it is much more practical (for example, if you would want to split the array based on amount of items to be container in each sub array):

试试这个更实用(例如,如果您希望根据每个子数组中要包含的项的数量来分割数组):

function chunk(arr, start, amount){
    var result = [], 
        i, 
        start = start || 0, 
        amount = amount || 500, 
        len = arr.length;

    do {
        //console.log('appending ', start, '-', start + amount, 'of ', len, '.');
        result.push(arr.slice(start, start+amount));
        start += amount;

    } while (start< len);

    return result;
};

and the use in your case:

在你的案例中使用:

var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17],
    chunked = chunk(arr, 0, Math.floor(arr.length/3)); //to get 4 nested arrays

console.log(chunked);

and another case:

和另一个案例:

var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17],
    chunked = chunk(arr, 0, 3); // to get 6 nested arrays each containing maximum of 3 items

console.log(chunked);

#5


0  

Underscore supports _.chunk() natively as of version 1.9.0.

下划线在1.9.0版本中支持_.chunk()。

let data = ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11", "a12", "a13"];
_.chunk(data, 4);
// [
//   ["a1", "a2", "a3", "a4"],
//   ["a5", "a6", "a7", "a8"],
//   ["a9", "a10", "a11", "a12"],
//   ["a13"]
// ]