how to split an array into equal length arrays in javascript [duplicate] - javascript

This question already has answers here:
Split array into chunks
(73 answers)
Closed 3 years ago.
How can I split an array into equal sized arrays:
var a = [1,2,3,4,5,6,7,8];
var b = a.split(2);
// b is equal to [[1,2],[3,4],[5,6],[7,8]];
// one possible way might be something like
[0,1,2,3].map(_ => a.slice(_* 2, _+2));

const chunk = (arr, size) => arr.reduce((carry, _, index, orig) => !(index % size) ? carry.concat([orig.slice(index,index+size)]) : carry, []);
console.log(chunk([1,2,3,4,5,6,7,8], 2))

For the second index for splicing, you need to add one and multiply with the length of the inner arrays.
var a = [1, 2, 3, 4, 5, 6, 7, 8],
b = [0, 1, 2, 3].map(i => a.slice(i * 2, (i + 1) * 2));
console.log(b);

Related

change index in one array in accordance with another one js [duplicate]

This question already has answers here:
Sort two arrays the same way
(12 answers)
Closed 18 days ago.
I need to change indexes of elements in arr a due to their indexes in arr b.
const a = [4,3,2,1,5];
const b = [1,2,3,4,5];
console.log(a) [1,2,3,4,5]
If you mean ordering array a according to array b, then you can do like this:
a.forEach((element,i) => {
// first get the index of a[i] from array b
const index = b.indexOf(a[i])
// then swap them
const temp = a[index];
a[index] = a[i];
a[i] = temp;
})
You could sort by using the other array as index. If this daoes not work with real data, please andd a small amount of data to highlight the problem.
const
a = [4, 3, 2, 1, 5],
b = [1, 2, 3, 4, 5];
a.sort((l, r) => b[l - 1] - b[r - 1]);
console.log(...a);

iterating through an array and performing a function [duplicate]

This question already has answers here:
Finding the sum of subarrays
(6 answers)
Closed 2 years ago.
I have my original array like this
[Array(3), Array(1), Array(3)]
each array has this
[2, 2, 3]
[1]
[2, 2, 2]
I want my final result to be something like this
[7,1,6]
Create a array sum function using Array.reduce(), and then map your array, and apply sum to each sub-array:
const sum = arr => arr.reduce((s, n) => s + n, 0)
const arr = [[2, 2, 3], [1], [2, 2, 2]]
const result = arr.map(sum)
console.log(result)

JavaScript Copy of Arrays [duplicate]

This question already has answers here:
Copy array by value
(39 answers)
Closed 2 years ago.
I'm trying to eliminate the minimum and maximum values from the below array and create 2 new arrays without the maximum and minimum values
array = [4, 6, 7, 8, 9]
const indOmin = 0,
indOm = 5
minArr = arr
maxArr = arr
minArr.forEach(cur => {
if (arr.indexOf(cur) === indOmin) {
minArr.splice(indOmin, 1)
}
})
maxArr.forEach(cur => {
if (arr.indexOf(cur) === indOm) {
maxArr.splice(indOm, 1)
}
})
When I use...
console.log(minArr)
console.log(maxArr)
...then in both cases it returns [6, 7, 8, 9].
But instead of...
minArr = arr
maxArr = arr
...if I use...
minArr = arr.map(cur => cur = cur)
maxArr = arr.map(cur => cur = cur)
... then the arrays return expected values.
[6, 7, 8, 9]
[4, 6, 7, 8]
Please help me understand why it doesn't work when I explicitly use the = operator (minArr = arr).
Change
minArr=arr; maxArr=arr;
To
minArr=[...arr]; maxArr=[...arr];
It doesn't work because array doesn't copy with = you had reference of same array in both minarr and maxarr.

Javascript, convert an array of double into an array of pair of doubles [duplicate]

This question already has answers here:
Split array into chunks
(73 answers)
Closed 2 years ago.
Suppose that you have an array of doubles in Javascript:
double_arr = [1, 2, 3, 4, 5, 6, 7, 8]
What is the most efficient way to convert it into an array of arrays with 2 doubles like above:
double_arr = [[1,2], [3,4], [5,6], [7,8]]
You can iterate skipping one index in each iteration, like this:
const double_arr = [1, 2, 3, 4, 5, 6, 7, 8];
const result = [];
for (let i = 0; i < double_arr.length; i += 2)
result.push([ double_arr[i], double_arr[i+1] ]);
console.log(result);

JavaScript: Slice an array into three (roughly) equal arrays [duplicate]

This question already has answers here:
Splitting a JS array into N arrays
(23 answers)
Closed 8 years ago.
How do I slice an array like this:
var a = [1, 2, 3, 4, 5, 6 , 7, 8];
into thirds (ie. three arrays, like this):
[1, 2, 3]
[4, 5, 6]
[7, 8]
Here's what I've got so far:
var first = a.slice(0, Math.ceil(a.length / 3));
var seconds = ???
var third = ???
This works, though it can be cleaned up:
var m, n;
var first, second, third;
m = Math.ceil(a.length / 3);
n = Math.ceil(2 * a.length / 3);
first = a.slice(0, m);
second = a.slice(m, n);
third = a.slice(n, a.length);
First, get the length. Nice and simple: a.length
Next, divide by three and round up. This will be the size of your pieces.
Finally, use a.slice() with appropriate arguments to get the resulting arrays.
Write some code using the above algorithm, and let us know if you have any more specific problems :)

Categories

Resources