如何使用 JavaScript 将长数组拆分为较小的数组
我有一个电子邮件数组(可以只是 1 封电子邮件,也可以是 100 封电子邮件),我需要使用 ajax 请求发送该数组(我知道该怎么做),但我只能发送一个包含以下内容的数组:其中包含 10 封或更少的电子邮件。因此,如果原始数组有 20 封电子邮件,我需要将它们分成 2 个数组,每组 10 封。或者,如果原始数组中有 15 封电子邮件,则 1 个包含 10 个电子邮件的数组,另一个包含 5 个电子邮件的数组。我正在使用 jQuery,那么最好的方法是什么?
I have an array of e-mails (it can be just 1 email, or 100 emails), and I need to send the array with an ajax request (that I know how to do), but I can only send an array that has 10 or less e-mails in it. So if there is an original array of 20 e-mails I will need to split them up into 2 arrays of 10 each. or if there are 15 e-mails in the original array, then 1 array of 10, and another array of 5. I'm using jQuery, what would be the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
作为一个函数
来使用
as a function
to use
使用递归
注意:现有数组即 myArr 将被修改。
using recursion
Note: The existing array i.e. myArr will be modified.
使用原型我们可以直接设置为数组类
using prototype we can set directly to array class
如果您知道要拆分的数字数组 (numGroups),则可以使用以下函数。
示例使用:
You can use the below function if you know the number array (numGroups) to be split.
Sample Use:
如果您想要一个不修改现有数组的方法,请尝试以下操作:
If you want a method that doesn't modify the existing array, try this:
您可以使用以下代码来实现所需的功能
请注意,它是一个长度为 22 的数组,然后 splitter 函数将其拆分为 2 个包含 10 个项目的较小数组和 1 个包含 2 个项目的数组。
You can use the following code to achieve the required functionality
Notice, it was an array of length 22 then the splitter function splits it into 2 smaller arrays of 10 items and 1 array of 2 items.
这是我认为简单的解决方案❤️
this is the easy solution i think❤️
您可以使用 map+filter 作为减少的替代方案:
You could use map+filter as an alternative to reduce:
不要使用 jquery...使用纯 javascript
你可以循环它来获得你想要的行为。
这将一次为您提供 10 个元素...如果您有 15 个元素,您将获得 1-10,即您想要的 11-15。
Don't use jquery...use plain javascript
You could loop this to get the behavior you want.
This would give you 10 elements at a time...if you have say 15 elements, you would get 1-10, the 11-15 as you wanted.
与
splice()
,slice()
对原始数组是无损的。Unlike
splice()
,slice()
is non-destructive to the original array.您可以使用洛达什:
https://lodash.com/docs
You can use lodash:
https://lodash.com/docs
只需循环数组,拼接它,直到全部消耗完。
输出
Just loop over the array, splicing it until it's all consumed.
output
Array.reduce 对于大型数组可能效率低下,尤其是使用 mod 运算符时。我认为更清晰(并且可能更容易阅读)的功能解决方案是这样的:
Array.reduce could be inefficient for large arrays, especially with the mod operator. I think a cleaner (and possibly easier to read) functional solution would be this:
假设您不想破坏原始数组,您可以使用这样的代码将长数组分解为较小的数组,然后您可以对其进行迭代:
Assuming you don't want to destroy the original array, you can use code like this to break up the long array into smaller arrays which you can then iterate over:
使用 ES6 生成器
虽然姗姗来迟,但 ES6 生成器 开辟了另一种巧妙的方法达到所要求的。
使其适用于无限生成器
使用相同的想法,您可以创建一个生成器,它还可以根据从另一个(可能是无限的)生成器函数检索的值生成无限数量的 n 大小的块。一旦需要,这对于延迟生成值来说非常方便,这会显着减少所需的内存,甚至可以用于生成可能无限/未知数量的块。
这是一个使用两个生成器的示例。
nextNaturalNumber()
是一个无限生成器,它始终返回下一个自然数。我在这里使用 ES2020bigint
数据类型,因此没有对值大小的限制(通过 JavaScript)。Using ES6 Generators
Late to the party, but ES6 generators opened up another neat way to achieve what is asked for.
Make it work for infinite Generators
Using the same idea you can create a generator which can also generate an infinite amount of
n-sized
chunks from values retrieved from another (possibly infinite) generator function. This can be very handy to lazy-generate values once they are required which significantly reduces the required memory or it can even be used to generate a possibly infinite/ unknown number of chunks.Here an example which uses two generators.
nextNaturalNumber()
is an infinite generator which always returns the next natural number. I am using the ES2020bigint
datatype here so there is no restriction (by JavaScript) for the size of the value.chunksFromIterable()
createsn-sized
chunks from an possibly infinite iterable.另一种实现:
注意 - 这不会修改原始数组。
或者,如果您更喜欢函数式的、100% 不可变的(尽管像上面那样就地改变确实没有什么坏处)和独立的方法:
Another implementation:
NB - This does not modify the original array.
Or, if you prefer a functional, 100% immutable (although there's really nothing bad in mutating in place like done above) and self-contained method:
作为@jyore的答案的补充,如果您仍然想保留原始数组:
As a supplement to @jyore's answer, and in case you still want to keep the original array:
我也想分享我的解决方案。它有点冗长,但也有效。
输出(格式化):
I would like to share my solution as well. It's a little bit more verbose but works as well.
Output (formatted):
您可以从一个空数组开始,然后将原始数组中包含所需范围的部分推入其中,同时从原始数组中减去,直到为空。
范围 3 的输出
范围 4 的输出
如果需要的话,这也适用于前置分页。
You can start with an empty array and push inside it sections with your desired range from the original array at the same time you are subtracting from your original array until is empty.
output for range 3
output for range 4
This is also good for a fronted pagination if want.
另一种方法:
这不会影响原始数组,因为使用切片创建的副本被传递到映射的“this”参数中。
Another method:
This doesn't affect the original array as a copy, made using slice, is passed into the 'this' argument of map.
另一种实现,使用 Array.reduce (我认为这是唯一缺少的一个!):
与上面的许多解决方案一样,这个是非破坏性的。当大小为 0 时返回空数组只是一种约定。如果省略
if
块,您会收到一个错误,这可能正是您想要的。Another implementation, using Array.reduce (I think it’s the only one missing!):
As many solutions above, this one’s non-destructive. Returning an empty array when the size is 0 is just a convention. If the
if
block is omitted you get an error, which might be what you want.更紧凑:
More compact:
你可以看一下这段代码。简单有效。
You can take a look at this code . Simple and Effective .
这是一个简单的衬里
Here is a simple one liner