如何拆分多头排列成更小的阵列,使用JavaScript [英] How to split a long array into smaller arrays, with 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?
推荐答案
不要使用jQuery ...用普通的JavaScript
Don't use jquery...use plain javascript
var a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
var b = a.splice(0,10);
//a is now [11,12,13,14,15];
//b is now [1,2,3,4,5,6,7,8,9,10];
您可以循环这个得到你想要的行为。
You could loop this to get the behavior you want.
var a = YOUR_ARRAY;
while(a.length) {
console.log(a.splice(0,10));
}
这会给你10元一次......如果你有说15个元素,你会得到1-10,11-15,你想要的。
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.
这篇关于如何拆分多头排列成更小的阵列,使用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!