如何将数组分割成两半,直到达到一定大小的块?

时间:2022-04-19 18:41:10

I have an array with a variable length that is greater than 3 and can be odd or even.

我有一个可变长度大于3的数组可以是奇数或偶数。

For example: var arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];

例如:var arr =[a,b,c,d,e,f,g,h,'我',' j '];

Now I want to split that array into halves.

现在我想把这个数组分成两半。

['a', 'b', 'c', 'd', 'e'] and ['f', 'g', 'h', 'i', 'j']

[a,b,c,d ',' e ']和[‘f’、‘g’、‘h’,‘我’,' j ']

Next I want to split those chunks into halves and to keep doing that until the chunks have a length of 3 or 2.

接下来,我要把这些块分割成两半,直到这些块的长度是3或2。

Finally I want to store those chunks in a new array.

最后,我想将这些块存储在一个新的数组中。

var newarr = [['a','b','c'],['d','e'],['f','g','h'],['i','j']];

var newarr =[[' a ',' b ',' c '],[' d ',' e '],[' f ',' g ',' h '],['我',' j ']];

How would I do this?

我该怎么做呢?

2 个解决方案

#1


4  

A self-suggesting way to do this is to use a recursive function F: for an input array arr if its length is <= 3 then the result is [arr], otherwise it is F(first_half_of_arr) concatenated with F(second_half_of_arr).

一种自我建议的方法是使用递归函数F:对于输入数组arr,如果它的长度为<= 3,那么结果是[arr],否则它是F(first_half_of_arr)与F(second_half_of_arr)连接在一起。

In code this translates to:

在代码中,这翻译为:

function recursiveSplit(arr) { 
    return arr.length <= 3 ? [arr] : 
        recursiveSplit(arr.slice(0, Math.ceil(arr.length / 2)))
        .concat(recursiveSplit(arr.slice(Math.ceil(arr.length / 2))));
}

You can exclude the Math.ceil calls, but if you do you are going to get the 2-length chunks before the 3-length ones in your result.

你可以把数学排除在外。ceil调用了,但是如果你这样做了,你会得到2个长度的块,而不是3个长度的块。

Now in practice an iterative implementation should be much more performant than a recursive one because it won't need to create and abandon small arrays entirely, so if this is expected to operate on large arrays you should probably stay away from recursion.

实际上,迭代实现的性能应该比递归实现高得多,因为它不需要完全创建和放弃小数组,所以如果期望它对大数组进行操作,那么您应该远离递归。

#2


0  

var arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
var arr1 = arr.splice(0, Math.ceil(arr.length / 2));

That will split an array in half, so now arr contain second half of original array and arr1 contains first half.

这将把数组分割成两半,所以现在arr包含原始数组的后半部分,arr1包含前半部分。

You should be able to just repeat in a while loop:

你应该能够在一段时间内重复:

while (arr > 3) {
    //continue splicing
}

If you don't think this answer is good enough (It probably makes no sense to anyone other than me, I am so tired right now) please just comment rather than disliking.

如果你觉得这个答案不够好(除了我,其他人可能觉得这个答案毫无意义,我现在太累了),请评论一下,而不是不喜欢。

#1


4  

A self-suggesting way to do this is to use a recursive function F: for an input array arr if its length is <= 3 then the result is [arr], otherwise it is F(first_half_of_arr) concatenated with F(second_half_of_arr).

一种自我建议的方法是使用递归函数F:对于输入数组arr,如果它的长度为<= 3,那么结果是[arr],否则它是F(first_half_of_arr)与F(second_half_of_arr)连接在一起。

In code this translates to:

在代码中,这翻译为:

function recursiveSplit(arr) { 
    return arr.length <= 3 ? [arr] : 
        recursiveSplit(arr.slice(0, Math.ceil(arr.length / 2)))
        .concat(recursiveSplit(arr.slice(Math.ceil(arr.length / 2))));
}

You can exclude the Math.ceil calls, but if you do you are going to get the 2-length chunks before the 3-length ones in your result.

你可以把数学排除在外。ceil调用了,但是如果你这样做了,你会得到2个长度的块,而不是3个长度的块。

Now in practice an iterative implementation should be much more performant than a recursive one because it won't need to create and abandon small arrays entirely, so if this is expected to operate on large arrays you should probably stay away from recursion.

实际上,迭代实现的性能应该比递归实现高得多,因为它不需要完全创建和放弃小数组,所以如果期望它对大数组进行操作,那么您应该远离递归。

#2


0  

var arr = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
var arr1 = arr.splice(0, Math.ceil(arr.length / 2));

That will split an array in half, so now arr contain second half of original array and arr1 contains first half.

这将把数组分割成两半,所以现在arr包含原始数组的后半部分,arr1包含前半部分。

You should be able to just repeat in a while loop:

你应该能够在一段时间内重复:

while (arr > 3) {
    //continue splicing
}

If you don't think this answer is good enough (It probably makes no sense to anyone other than me, I am so tired right now) please just comment rather than disliking.

如果你觉得这个答案不够好(除了我,其他人可能觉得这个答案毫无意义,我现在太累了),请评论一下,而不是不喜欢。