How to rearrange an array by indices array? - javascript
Given an array arr and an array of indices ind, I'd like to rearrange arr in-place to satisfy the given indices. For example:
var arr = ["A", "B", "C", "D", "E", "F"];
var ind = [4, 0, 5, 2, 1, 3];
rearrange(arr, ind);
console.log(arr); // => ["B", "E", "D", "F", "A", "C"]
Here is a possible solution that uses O(n) time and O(1) space, but mutates ind:
function swap(arr, i, k) {
var temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
function rearrange(arr, ind) {
for (var i = 0, len = arr.length; i < len; i++) {
if (ind[i] !== i) {
swap(arr, i, ind[i]);
swap(ind, i, ind[i]);
}
}
}
What would be the optimal solution if we are limited to O(1) space and mutating ind is not allowed?
Edit: The algorithm above is wrong. See this question.
This is the "sign bit" solution.
Given that this is a JavaScript question and the numerical literals specified in the ind array are therefore stored as signed floats, there is a sign bit available in the space used by the input.
This algorithm cycles through the elements according to the ind array and shifts the elements into place until it arrives back to the first element of that cycle. It then finds the next cycle and repeats the same mechanism.
The ind array is modified during execution, but will be restored to its original at the completion of the algorithm. In one of the comments you mentioned that this is acceptable.
The ind array consists of signed floats, even though they are all non-negative (integers). The sign-bit is used as an indicator for whether the value was already processed or not. In general, this could be considered extra storage (n bits, i.e. O(n)), but as the storage is already taken by the input, it is not additional acquired space. The sign bits of the ind values which represent the left-most member of a cycle are not altered.
Edit: I replaced the use of the ~ operator, as it does not produce the desired results for numbers equal or greater than 231, while JavaScript should support numbers to be used as array indices up to at least 232 - 1. So instead I now use k = -k-1, which is the same, but works for the whole range of floats that is safe for use as integers. Note that as alternative one could use a bit of the float's fractional part (+/- 0.5).
Here is the code:
var arr = ["A", "B", "C", "D", "E", "F"];
var ind = [4, 0, 5, 2, 1, 3];
rearrange(arr, ind);
console.log('arr: ' + arr);
console.log('ind: ' + ind);
function rearrange(arr, ind) {
var i, j, buf, temp;
for (j = 0; j < ind.length; j++) {
if (ind[j] >= 0) { // Found a cycle to resolve
i = ind[j];
buf = arr[j];
while (i !== j) { // Not yet back at start of cycle
// Swap buffer with element content
temp = buf;
buf = arr[i];
arr[i] = temp;
// Invert bits, making it negative, to mark as visited
ind[i] = -ind[i]-1;
// Visit next element in cycle
i = -ind[i]-1;
}
// dump buffer into final (=first) element of cycle
arr[j] = buf;
} else {
ind[j] = -ind[j]-1; // restore
}
}
}
Although the algorithm has a nested loop, it still runs in O(n) time: the swap happens only once per element, and also the outer loop visits every element only once.
The variable declarations show that the memory usage is constant, but with the remark that the sign bits of the ind array elements -- in space already allocated by the input -- are used as well.
Index array defines a permutation. Each permutation consists of cycles. We could rearrange given array by following each cycle and replacing the array elements along the way.
The only problem here is to follow each cycle exactly once. One possible way to do this is to process the array elements in order and for each of them inspect the cycle going through this element. If such cycle touches at least one element with lesser index, elements along this cycle are already permuted. Otherwise we follow this cycle and reorder the elements.
function rearrange(values, indexes) {
main_loop:
for (var start = 0, len = indexes.length; start < len; start++) {
var next = indexes[start];
for (; next != start; next = indexes[next])
if (next < start) continue main_loop;
next = start;
var tmp = values[start];
do {
next = indexes[next];
tmp = [values[next], values[next] = tmp][0]; // swap
} while (next != start);
}
return values;
}
This algorithm overwrites each element of given array exactly once, does not mutate the index array (even temporarily). Its worst-case complexity is O(n2). But for random permutations its expected complexity is O(n log n) (as noted in comments for related answer).
This algorithm could be optimized a little bit. Most obvious optimization is to use a short bitset to keep information about several indexes ahead of current position (whether they are already processed or not). Using a single 32-or-64-bit word to implement this bitset should not violate O(1) space requirement. Such optimization would give small but noticeable speed improvement. Though it does not change worst case and expected asymptotic complexities.
To optimize more, we could temporarily use the index array. If elements of this array have at least one spare bit, we could use it to maintain a bitset allowing us to keep track of all processed elements, which results in a simple linear-time algorithm. But I don't think this could be considered as O(1) space algorithm. So I would assume that index array has no spare bits.
Still the index array could give us some space (much larger then a single word) for look-ahead bitset. Because this array defines a permutation, it contains much less information than arbitrary array of the same size. Stirling approximation for ln(n!) gives n ln n bits of information while the array could store n log n bits. Difference between natural and binary logarithms gives us to about 30% of potential free space. Also we could extract up to 1/64 = 1.5% or 1/32 = 3% free space if size of the array is not exactly a power-of-two, or in other words, if high-order bit is only partially used. (And these 1.5% could be much more valuable than guaranteed 30%).
The idea is to compress all indexes to the left of current position (because they are never used by the algorithm), use part of free space between compressed data and current position to store a look-ahead bitset (to boost performance of the main algorithm), use other part of free space to boost performance of the compression algorithm itself (otherwise we'll need quadratic time for compression only), and finally uncompress all the indexes back to original form.
To compress the indexes we could use factorial number system: scan the array of indexes to find how many of them are less than current index, put the result to compressed stream, and use available free space to process several values at once.
The downside of this method is that most of free space is produced when algorithm comes to the array's end while this space is mostly needed when we are at the beginning. As a result, worst-case complexity is likely to be only slightly less than O(n2). This could also increase expected complexity if not this simple trick: use original algorithm (without compression) while it is cheap enough, then switch to the "compressed" variant.
If length of the array is not a power of 2 (and we have partially unused high-order bit) we could just ignore the fact that index array contains a permutation, and pack all indexes as if in base-n numeric system. This allows to greatly reduce worst-case asymptotic complexity as well as speed up the algorithm in "average case".
This proposal utilizes the answer of Evgeny Kluev.
I made an extension for faster processing, if all elements are already treated, but the index has not reached zero. This is done with an additional variable count, which counts down for every replaced element. This is used for leaving the main loop if all elements are at right position (count = 0).
This is helpful for rings, like in the first example with
["A", "B", "C", "D", "E", "F"]
[ 4, 0, 5, 2, 1, 3 ]
index 5: 3 -> 2 -> 5 -> 3
index 4: 1 -> 0 -> 4 -> 1
Both rings are at first two loops rearranged and while each ring has 3 elements, the count is now zero. This leads to a short circuit for the outer while loop.
function rearrange(values, indices) {
var count = indices.length, index = count, next;
main: while (count && index--) {
next = index;
do {
next = indices[next];
if (next > index) continue main;
} while (next !== index)
do {
next = indices[next];
count--;
values[index] = [values[next], values[next] = values[index]][0];
} while (next !== index)
}
}
function go(values, indices) {
rearrange(values, indices);
console.log(values);
}
go(["A", "B", "C", "D", "E", "F"], [4, 0, 5, 2, 1, 3]);
go(["A", "B", "C", "D", "E", "F"], [1, 2, 0, 4, 5, 3]);
go(["A", "B", "C", "D", "E", "F"], [5, 0, 1, 2, 3, 4]);
go(["A", "B", "C", "D", "E", "F"], [0, 1, 3, 2, 4, 5]);
This answer has been updated to satisfy OP's conditions
In this answer there are no temp arrays and ind array doesn't get re-ordered or sorted by any means. All replacing operations are done in a single pass. getItemIndex function receives only a shallow portion of the ind array to work with. It's just done by harnessing all the information hidden in the ind array.
It's key to understand that the ind array keeps all history for us.
We have the following information by examining the ind array.
By looking at the items we find the index map of the corresponding item at arr array.
Each items index tells us how many swaps done before. We get history.
Each items index also tells if there was a previous swap related with the current index position where did the previous element go. We can do like ind.indexOf(i); Anyways here is the code;
I have added a few functions like Array.prototype.swap() to make the code interpreted easier. Here is the code.
Array.prototype.swap = function(i,j){
[this[i],this[j]] = [this[j],this[i]];
return this;
};
function getItemIndex(a,i){
var f = a.indexOf(i);
return f !=-1 ? getItemIndex(a,f) : i;
}
function sort(arr,ind){
ind.forEach((n,i,x) => x.indexOf(i) > i ? arr.swap(i,x[i]) // item has not changed before so we can swap
: arr.swap(getItemIndex(ind.slice(0,i),i),x[i])); // item has gone to somwhere in previous swaps get it's index and swap
return arr;
}
var arr = ["A", "B", "C", "D", "E", "F"],
ind = [4, 0, 5, 2, 1, 3];
console.log(sort(arr,ind),ind);
Ok the very final version of this code is this. It is very simplified and includes a test case with 26 letters. Each time you run you will get a different purely random unique indexes map.
Array.prototype.swap = function(i,j){
i !=j && ([this[i],this[j]] = [this[j],this[i]]);
return this;
};
Array.prototype.shuffle = function(){
var i = this.length,
j;
while (i > 1) {
j = ~~(Math.random()*i--);
[this[i],this[j]] = [this[j],this[i]];
}
return this;
};
var arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"],
ind = (new Array(arr.length)).fill("").map((e,i) => e = i).shuffle();
console.log(JSON.stringify(arr));
console.log(JSON.stringify(ind));
function getItemIndex(a,i,j){
var f = a.indexOf(i);
return f < j ? getItemIndex(a,f,j) : i;
}
function sort(arr,ind){
ind.forEach((n,i,x) => arr.swap(getItemIndex(ind,i,i),n));
return arr;
}
console.log(JSON.stringify(sort(arr,ind)));
console.log(JSON.stringify(ind));
So ok as per the comment from Trincot here it goes with an iterative getItemIndex() function.
Array.prototype.swap = function(i,j){
i !=j && ([this[i],this[j]] = [this[j],this[i]]);
return this;
};
Array.prototype.shuffle = function(){
var i = this.length,
j;
while (i > 1) {
j = ~~(Math.random()*i--);
[this[i],this[j]] = [this[j],this[i]];
}
return this;
};
var arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"],
ind = (new Array(arr.length)).fill("").map((e,i) => e = i).shuffle();
console.log(JSON.stringify(arr));
console.log(JSON.stringify(ind));
function getItemIndex(a,i){
var f = a.indexOf(i),
j;
if (f >= i) return i; // this element hasn't been moved before.
while (f < i) { // this element has been swapped so get this elements current index
j = f;
f = a.indexOf(f);
}
return j;
}
function sort(arr,ind){
ind.forEach((n,i,x) => arr.swap(getItemIndex(ind,i),n));
return arr;
}
console.log(JSON.stringify(sort(arr,ind)));
console.log(JSON.stringify(ind));
var arr = ["A", "B", "C", "D", "E", "F"];
var ind = [4, 0, 5, 2, 1, 3];
function rearrange(arr, ind){
var map = [];
for (var i = 0; i < arr.length; i++) map[ind[i]] = arr[i];
for (var i = 0; i < arr.length; i++) arr[i] = map[i];
}
rearrange(arr, ind);
console.log(arr);
That works but, because I'm not a smart developper, I assume it's probably not the fastest algorithm.
Below, one may find a PARTIAL solution for a case when we have only ONE cycle, i.e.
var arr = ["A", "B", "C", "D", "E", "F"];
var ind = [4, 2, 5, 0, 1, 3];
function rearrange( i, arr, ind, temp ){
if( temp ){
if( arr[ind[i]] ){
var temp2 = arr[ind[i]];
arr[ind[i]] = temp;
rearrange( ind[i], arr, ind, temp2 );
}
else{ // cycle
arr[ind[i]] = temp;
// var unvisited_index = ...;
// if( unvisited_index ) rearrange( unvisited_index, arr, ind, "" );
}
}
else{
if( i == ind[i] ){
if( i < arr.length ) rearrange( i + 1, arr, ind, temp );
}
else{
temp = arr[ind[i]];
arr[ind[i]]=arr[i];
arr[i] = "";
i = ind[i];
rearrange(i, arr, ind, temp );
}
}
}
rearrange( 0, arr, ind, "" );
To make this solution to work for a general case, we need to find total numbers of unique cycles and one index from each of them.
For the OP example:
var arr = ["A", "B", "C", "D", "E", "F"];
var ind = [4, 0, 5, 2, 1, 3];
There are 2 unique cycles:
4 -> 1 -> 0 -> 4
5 -> 3 -> 2 -> 5
If one runs
rearrange( 0, arr, ind, "" );
rearrange( 5, arr, ind, "" );
S(he) will get desirable output for the OP problem.
I'm not sure on the time, but the map function does appear to do what was requested. It's an option, but since I don't know the inner workings of .map then I can't say for sure this is what you're looking for.
var arr = ["A", "B", "C", "D", "E", "F"];
var ind = [4, 0, 5, 2, 1, 3];
arr = ind.map(function(value)
{ return arr[value]; });
Another solution that doesn't use the map function could look something like this:
var arr = ["A", "B", "C", "D", "E", "F"];
var ind = [4, 0, 5, 2, 1, 3];
var temp = [];
for (var i = 0, ind_length = ind.length; i < ind_length; i++)
{
var set_index = ind[i];
temp.push(arr[set_index]);
delete arr[set_index];
}
arr = temp;
This makes good use of space by using the delete option which also keeps the indexes from shifting. Since it's only doing one loop I imagine the execution is rather fast. Since the commands are very basic and simple this should be a viable solution. It's not quite what was asked which was a swap with no extra space used, but it comes pretty close. I'm new to answering questions like this one so please... constructive criticism.
Try this:
var result = new Array(5);
for (int i = 0; i < result.length; i++) {
result[i] = arr[ind[i]];
}
console.log(arr);
I am using ind as indexes in it's own order
var arr = ["A", "B", "C", "D", "E", "F"];
var ind = [4, 0, 5, 2, 1, 3];
var obj = {}
for(var i=0;i<arr.length;i++)
obj[ind[i]]=arr[i];
console.log(obj);
Working Fiddle
Related
Why is splice not removing elements from my array?
I am creating a roulette game that displays random items from different arrays when the wheel lands on a specific category. So far everything works except when the wheel lands on a category, it selects the same random item from the correct array over and over again. I am trying to use math.random and the splice method to randomly select an item from an array, and remove that item so only new, random items from the array can be displayed after, but it hasn't worked.
I rearranged the input arrays into an array of arrays (5 arrays of 9 elements = 45). I'm guessing that you want the whole thing shuffled. const log = data => console.log(JSON.stringify(data)); let zones = [ ["π", "π", "π―", "π", "π", "π©", "π°", "π", "π΄"], ["π", "π", "π", "π", "π£", "π²", "π°", "π", "πΏ"], ["π", "π", "π―", "π", "π", "π©", "π°", "π", "π΄"], ["π", "π", "π", "π", "π£", "π²", "π°", "π", "πΏ"], ["π", "π", "π―", "π", "π", "π©", "π°", "π", "π΄"] ]; let zoneSize = zones.length * zones[0].length; let symbolZones = []; for (let i = zoneSize; i > 0; i--) { let deg = zones.flatMap(z => z.splice(Math.floor(Math.random() * z.length), 1)); if(deg.length > 0) { symbolZones.push(deg); } } log(`Original Array zones`); log(zones); log(`New Array symbolZones`); log(symbolZones); .as-console-row-code { display: block; width: 100%; overflow-wrap: anywhere; } .as-console-row::after { content: ''!important }
I don't know how the rest of the code is, but if you always get the same value over and over again it could be that you're not re-running the code that gets the values. Try wrapping the logic for retrieving the symbolZones in a function: function getSymbolZones() { const symbolZones = []; symbolZones[1] = a[Math.floor(Math.random()*a.length)]; symbolZones[2] = b[Math.floor(Math.random()*b.length)]; symbolZones[3] = c[Math.floor(Math.random()*c.length)]; symbolZones[4] = d[Math.floor(Math.random()*d.length)]; symbolZones[5] = e[Math.floor(Math.random()*e.length)]; symbolZones[6] = f[Math.floor(Math.random()*f.length)]; symbolZones[7] = g[Math.floor(Math.random()*g.length)]; symbolZones[8] = h[Math.floor(Math.random()*h.length)]; return symbolZones; } and then use it in the handleWin function. const handleWin = (actualDeg) => { const symbolZones = getSymbolZones(); const winningSymbolNr = Math.ceil(actualDeg / zoneSize); display.innerHTML = symbolZones[winningSymbolNr]; } P.S. I understand that you start the array at index 1 because that's the smallest zone you can get from the operation 45 / 45. But if I were you I'd start the index from 0 and just subtract 1 when accessing the array: symbolZones[winningSymbolNr - 1]. This way you don't get weird bugs when trying to loop through an array that has an empty first index.
Other than using array, I would suggest you use object and a for loop to make the code easier to execute. This code should work: let deg = 0; // The 360 degree wheel has 8 zones, so each zone is 45 degrees let zoneSize = 45; let symbol = { a: ["a", "b", "c", "d", "e"], b: ["f", "g", "h", "i", "j"], c: ["a", "b", "c", "d", "e"], d: ["f", "g", "h", "i", "j"], e: ["a", "b", "c", "d", "e"], f: ["f", "g", "h", "i", "j"], g: ["a", "b", "c", "d", "e"], h: ["f", "g", "h", "i", "j"], } // zones for each 8 categories let ran = Math.floor(Math.random() * Object.keys(symbol).length) console.log(Object.values(symbol)[ran]) //Random select a zone from above 8 let selectedzone = Object.values(symbol)[ran] //Random select an item from selected zone let index = Math.floor(Math.random() * selectedzone.length) let symbolZones = selectedzone[index] console.log(symbolZones) const handleWin = (actualDeg) => { const winningSymbolNr = Math.ceil(actualDeg / zoneSize); display.innerHTML = symbolZones[winningSymbolNr]; }
JS join arrays with replacement
There are 2 arrays: var arr1 = ["a", "b", "c"]; var arr2 = ["k", ,"l","m","n"]; Need some standart function that returns: var arr3=["k","b","l"]; This one is too slow: function join_arrays(arr1,arr2) { var arr3=[]; for(var i=0;i<arr1.length;i++) if(arr2[i]==undefined) arr3[i]=arr1[i]; else arr3[i]=arr2[i]; return arr3; }
You could do the following: var arr1 = ["a", "b", "c"]; var arr2 = ["k",undefined,"l","m","n"]; var arr3 = []; function join_arrays(arr1,arr2){ arr3 = arr2; var i = arr3.indexOf(undefined); while(i !=- 1){ arr3[i] = arr1[i]; i = arr3.indexOf(undefined); } return arr3; } However there is a little caveat here, as far as my testing in JSBin showed me, which is that you have to have set the empty values that are gonna be replaced to undefined explicitly as I did in my example. If this is not optimal for you, there might be a better way than what I showed here. Hopefully this runs faster than your code, as it will only go through the loop as many times as needed to do the replacements and will fill arr3 with arr2 right away. UPDATE: Bear in mind that, while the above function works, it is unsafe because when the second array has empty elements in an index that is not present in the first one, it will cause an error. Therefore you could do something like this: function join_arrays(arr1,arr2){ arr3=arr2; var i = arr3.indexOf(undefined); while(i!=-1 && i<arr1.length){ arr3[i]=arr1[i]; i=arr3.indexOf(undefined); } return arr3; } So for var arr2 = ["k",undefined,undefined,"l","m","n",undefined] result will be ["k", "b", "c", "l", "m", "n", undefined] with this method, instead of getting an error or infinite loop!
With use standard functions, it's can be faster. let arr1 = ["a", "b", "c"]; let arr2 = ["k", ,"l","m","n"]; arr1.map((e,i)=>arr2[i]==undefined?e:arr2[i])
Probably this single liner should do your job. var arr3 = arr1.forEach((e,i) => arr2[i] === void 0 ? arr3.push(e): arr3.push(arr2[i]));
Removing Element From Array of Arrays with Regex
I'm using regex to test certain elements in an array of arrays. If an inner array doesn't follow the desired format, I'd like to remove it from the main/outer array. The regex I'm using is working correctly. I am not sure why it isn't removing - can anyone advise or offer any edits to resolve this problem? for (var i = arr.length-1; i>0; i--) { var a = /^\w+$/; var b = /^\w+$/; var c = /^\w+$/; var first = a.test(arr[i][0]); var second = b.test(arr[i][1]); var third = c.test(arr[i][2]); if ((!first) || (!second) || (!third)){ arr.splice(i,1); }
When you cast splice method on an array, its length is updated immediately. Thus, in future iterations, you will probably jump over some of its members. For example: var arr = ['a','b','c','d','e','f','g'] for(var i = 0; i < arr.length; i++) { console.log(i, arr) if(i%2 === 0) { arr.splice(i, 1) // remove elements with even index } } console.log(arr) It will output: 0 ["a", "b", "c", "d", "e", "f", "g"] 1 ["b", "c", "d", "e", "f", "g"] 2 ["b", "c", "d", "e", "f", "g"] 3 ["b", "c", "e", "f", "g"] 4 ["b", "c", "e", "f", "g"] ["b", "c", "e", "f"] My suggestion is, do not modify the array itself if you still have to iterate through it. Use another variable to save it. var arr = ['a','b','c','d','e','f','g'] var another = [] for(var i = 0; i < arr.length; i++) { if(i%2) { another.push(arr[i]) // store elements with odd index } } console.log(another) // ["b", "d", "f"] Or you could go with Array.prototype.filter, which is much simpler: arr.filter(function(el, i) { return i%2 // store elements with odd index }) It also outputs: ["b", "d", "f"]
Your code seems to work to me. The code in your post was missing a } to close the for statement but that should have caused the script to fail to parse and not even run at all. I do agree with Leo that it would probably be cleaner to rewrite it using Array.prototype.filter though. The code in your question would look something like this as a filter: arr = arr.filter(function (row) { return /^\w+$/.test(row[0]) && /^\w+$/.test(row[1]) && /^\w+$/.test(row[2]); }); jsFiddle I'm assuming it is 3 different regular expressions in your actual code, if they are all identical in your code you can save a little overhead by defining the RegExp literal once: arr = arr.filter(function (row) { var rxIsWord = /^\w+$/; return rxIsWord.test(row[0]) && rxIsWord.test(row[1]) && rxIsWord.test(row[2]); });
How to split a long array into smaller arrays, with JavaScript
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?
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)); } 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.
var size = 10; var arrayOfArrays = []; for (var i=0; i<bigarray.length; i+=size) { arrayOfArrays.push(bigarray.slice(i,i+size)); } console.log(arrayOfArrays); Unlike splice(), slice() is non-destructive to the original array.
Just loop over the array, splicing it until it's all consumed. var a = ['a','b','c','d','e','f','g'] , chunk while (a.length > 0) { chunk = a.splice(0,3) console.log(chunk) } output [ 'a', 'b', 'c' ] [ 'd', 'e', 'f' ] [ 'g' ]
You can use lodash: https://lodash.com/docs _.chunk(['a', 'b', 'c', 'd'], 2); // β [['a', 'b'], ['c', 'd']]
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: const chunkArray = (arr, size) => arr.length > size ? [arr.slice(0, size), ...chunkArray(arr.slice(size), size)] : [arr];
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: var longArray = []; // assume this has 100 or more email addresses in it var shortArrays = [], i, len; for (i = 0, len = longArray.length; i < len; i += 10) { shortArrays.push(longArray.slice(i, i + 10)); } // now you can iterate over shortArrays which is an // array of arrays where each array has 10 or fewer // of the original email addresses in it for (i = 0, len = shortArrays.length; i < len; i++) { // shortArrays[i] is an array of email addresss of 10 or less }
Another implementation: const arr = ["H", "o", "w", " ", "t", "o", " ", "s", "p", "l", "i", "t", " ", "a", " ", "l", "o", "n", "g", " ", "a", "r", "r", "a", "y", " ", "i", "n", "t", "o", " ", "s", "m", "a", "l", "l", "e", "r", " ", "a", "r", "r", "a", "y", "s", ",", " ", "w", "i", "t", "h", " ", "J", "a", "v", "a", "S", "c", "r", "i", "p", "t"]; const size = 3; const res = arr.reduce((acc, curr, i) => { if ( !(i % size) ) { // if index is 0 or can be divided by the `size`... acc.push(arr.slice(i, i + size)); // ..push a chunk of the original array to the accumulator } return acc; }, []); // => [["H", "o", "w"], [" ", "t", "o"], [" ", "s", "p"], ["l", "i", "t"], [" ", "a", " "], ["l", "o", "n"], ["g", " ", "a"], ["r", "r", "a"], ["y", " ", "i"], ["n", "t", "o"], [" ", "s", "m"], ["a", "l", "l"], ["e", "r", " "], ["a", "r", "r"], ["a", "y", "s"], [",", " ", "w"], ["i", "t", "h"], [" ", "J", "a"], ["v", "a", "S"], ["c", "r", "i"], ["p", "t"]] 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: function splitBy(size, list) { return list.reduce((acc, curr, i, self) => { if ( !(i % size) ) { return [ ...acc, self.slice(i, i + size), ]; } return acc; }, []); }
As a supplement to #jyore's answer, and in case you still want to keep the original array: var originalArray = [1,2,3,4,5,6,7,8]; var splitArray = function (arr, size) { var arr2 = arr.slice(0), arrays = []; while (arr2.length > 0) { arrays.push(arr2.splice(0, size)); } return arrays; } splitArray(originalArray, 2); // originalArray is still = [1,2,3,4,5,6,7,8];
I would like to share my solution as well. It's a little bit more verbose but works as well. var data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; var chunksize = 4; var chunks = []; data.forEach((item)=>{ if(!chunks.length || chunks[chunks.length-1].length == chunksize) chunks.push([]); chunks[chunks.length-1].push(item); }); console.log(chunks); Output (formatted): [ [ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12], [13, 14, 15 ] ]
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. const originalArr = [1,2,3,4,5,6,7,8,9,10,11]; const splittedArray = []; while (originalArr.length > 0) { splittedArray.push(originalArr.splice(0,range)); } output for range 3 splittedArray === [[1,2,3][4,5,6][7,8,9][10,11]] output for range 4 splittedArray === [[1,2,3,4][5,6,7,8][9,10,11]] This is also good for a fronted pagination if want.
Another method: var longArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var size = 2; var newArray = new Array(Math.ceil(longArray.length / size)).fill("") .map(function() { return this.splice(0, size) }, longArray.slice()); // newArray = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]; This doesn't affect the original array as a copy, made using slice, is passed into the 'this' argument of map.
Another implementation, using Array.reduce (I think itβs the only one missing!): const splitArray = (arr, size) => { if (size === 0) { return []; } return arr.reduce((split, element, index) => { index % size === 0 ? split.push([element]) : split[Math.floor(index / size)].push(element); return split; }, []); }; 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: const chunk = (xs, size) => xs.map((_, i) => (i % size === 0 ? xs.slice(i, i + size) : null)).filter(Boolean); // Usage: const sampleArray = new Array(33).fill(undefined).map((_, i) => i); console.log(chunk(sampleArray, 5));
function chunkArrayInGroups(arr, size) { var newArr=[]; for (var i=0; i < arr.length; i+= size){ newArr.push(arr.slice(i,i+size)); } return newArr; } chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3);
Using ES6 Generators Late to the party, but ES6 generators opened up another neat way to achieve what is asked for. /** * Returns chunks of size n. * #param {Array<any>} array any array * #param {number} n size of chunk */ function* chunks(array, n){ for(let i = 0; i < array.length; i += n) yield array.slice(i, i + n); } const result = [...chunks([1, 2, 3, 4, 5, 6, 7, 8 , 9, 10], 3)]; console.log(result); .as-console-wrapper { max-height: 100% !important; top: 0; } 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 ES2020 bigint datatype here so there is no restriction (by JavaScript) for the size of the value. chunksFromIterable() creates n-sized chunks from an possibly infinite iterable. /** * Returns chunks of size n for a possibly infinite iterator. * n must be >= 1 * #param {Iterable<any>} iterable any array * #param {number} n size of chunk for n >= 1 */ function* chunksFromIterable(iterable, n){ let arr = []; let i = n; for (const value of iterable) { if(i <= 0) { // another chunk of size n is filled => return chunk yield arr; arr = []; // create new empty array i = n; }; arr.push(value); i--; } // in case the iterable is not infinite check if there are still values in the array and return them if necessary if(arr.length > 0) yield arr; } /** * Infinite iterator which always gets the next natural number. */ function* nextNaturalNumber(){ let i = 0n; while(true) { i += 1n; yield i; } } console.log("Finite iterable:"); // this version can now be used using the for ... of loop for(const threeNaturalNumbers of chunksFromIterable([1, 2, 3, 4, 5, 6, 7, 8 , 9, 10], 3)){ console.log(threeNaturalNumbers); } console.log("Infinite iterable:"); // and it can also be used for this infinite generator for(const threeNaturalNumbers of chunksFromIterable(nextNaturalNumber(), 3)){ printBigIntArray(threeNaturalNumbers); if(threeNaturalNumbers[0] > 30) break; // end here to avoid an infinite loop } // helper function to print array of bigints as this does not seem to be working for snippets function printBigIntArray(arr){ console.log(`[${arr.join(", ")}]`); } .as-console-wrapper { max-height: 100% !important; top: 0; }
You can take a look at this code . Simple and Effective . function chunkArrayInGroups(array, 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; } chunkArrayInGroups(["a", "b", "c", "d"], 2);
Here is a simple one liner var segment = (arr, n) => arr.reduce((r,e,i) => i%n ? (r[r.length-1].push(e), r) : (r.push([e]), r), []), arr = Array.from({length: 31}).map((_,i) => i+1); console.log(segment(arr,7));
as a function var arrayChunk = function (array, chunkSize) { var arrayOfArrays = []; if (array.length <= chunkSize) { arrayOfArrays.push(array); } else { for (var i=0; i<array.length; i+=chunkSize) { arrayOfArrays.push(array.slice(i,i+chunkSize)); } } return arrayOfArrays; } to use arrayChunk(originalArray, 10) //10 being the chunk size.
using recursion let myArr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]; let size = 4; //Math.sqrt(myArr.length); --> For a n x n matrix let tempArr = []; function createMatrix(arr, i) { if (arr.length !== 0) { if(i % size == 0) { tempArr.push(arr.splice(0,size)) } createMatrix(arr, i - 1) } } createMatrix(myArr, myArr.length); console.log(tempArr); Note: The existing array i.e. myArr will be modified.
using prototype we can set directly to array class Array.prototype.chunk = function(n) { if (!this.length) { return []; } return [this.slice(0, n)].concat(this.slice(n).chunk(n)); }; console.log([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].chunk(5));
let original = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]; let size = 5; let fragments = Array.from(Array(Math.ceil(a.length / size))).map((_,index) => a.slice(index * size,(index + 1) * size))
If you want a method that doesn't modify the existing array, try this: let oldArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; let newArray = []; let size = 3; // Size of chunks you are after let j = 0; // This helps us keep track of the child arrays for (var i = 0; i < oldArray.length; i++) { if (i % size === 0) { j++ } if(!newArray[j]) newArray[j] = []; newArray[j].push(oldArray[i]) }
function chunkArrayInGroups(arr, size) { var newArr=[]; for (var i=0; arr.length>size; i++){ newArr.push(arr.splice(0,size)); } newArr.push(arr.slice(0)); return newArr; } chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3);
You can use the following code to achieve the required functionality const splitter = (arr, splitBy, cache = []) => { const tmp = [...arr] while (tmp.length) cache.push(tmp.splice(0, splitBy)) return cache } const split = splitter([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21], 10) console.log(split); 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.
let a = [1, 2, 3, 4, 6, 7, 8, 9, 10, 11]; let _splitCount = 3; let b = a.reduce((acc, curr, index) => { if (index % _splitCount === 0) { acc.push([curr]); } else { acc[acc.length - 1].push(curr); } return acc; }, []); this is the easy solution i thinkβ€οΈ
You can use the below function if you know the number array (numGroups) to be split. function createGroups(arr, numGroups) { const perGroup = Math.ceil(arr.length / numGroups); return new Array(numGroups) .fill('') .map((_, i) => arr.slice(i * perGroup, (i + 1) * perGroup)); } Sample Use: createGroups([0, 1, 2, 3, 4, 5, 6], 3); //arr = [0, 1, 2, 3, 4, 5, 6] and numGroups = 3
split an array into two based on a index in javascript
I have an array with a list of objects. I want to split this array at one particular index, say 4 (this in real is a variable). I want to store the second part of the split array into another array. Might be simple, but I am unable to think of a nice way to do this.
Use slice, as such: var ar = [1,2,3,4,5,6]; var p1 = ar.slice(0,4); var p2 = ar.slice(4);
You can use Array#splice to chop all elements after a specified index off the end of the array and return them: x = ["a", "b", "c", "d", "e", "f", "g"]; y = x.splice(3); console.log(x); // ["a", "b", "c"] console.log(y); // ["d", "e", "f", "g"]
use slice: var bigOne = [0,1,2,3,4,5,6]; var splittedOne = bigOne.slice(3 /*your Index*/);
I would recommend to use slice() like below ar.slice(startIndex,length); or ar.slice(startIndex); var ar = ["a","b","c","d","e","f","g"]; var p1 = ar.slice(0,3); var p2 = ar.slice(3); console.log(p1); console.log(p2);
const splitAt = (i, arr) => { const clonedArray = [...arr]; return [clonedArray.splice(0, i), clonedArray]; } const [left, right] = splitAt(1, [1,2,3,4]) console.log(left) // [1] console.log(right) // [2,3,4] const [left1, right1] = splitAt(-1, [1,2,3,4]) console.log(left1) // [] console.log(right1) // [1,2,3,4] const [left2, right2] = splitAt(5, [1,2,3,4]) console.log(left1) // [1,2,3,4] console.log(right1) // [] Some benefits compared to other solutions: You can get the result with a one liner When split index is underflow or overflow, the result is still correct. slice will not behave correctly. It does not mutate the original array. Some splice based solutions did. There is only 1 splice operation, rather than 2 slice operations. But you need to benchmark to see if there is actual performance difference.
You can also use underscore/lodash wrapper: var ar = [1,2,3,4,5,6]; var p1 = _.first(ar, 4); var p2 = _.rest(ar, 4);
Simple one function from lodash: const mainArr = [1,2,3,4,5,6,7] const [arr1, arr2] = _.chunk(mainArr, _.round(mainArr.length / 2));
const splitArrayByIndex = (arr, index) => { if (index > 0 && index < arr.length) { return [arr.slice(0, index), arr.slice(-1 * (arr.length - index))] } } const input = ['a', 'x', 'c', 'r'] const output = splitArrayByIndex(input, 2) console.log({ input, output })