This question already has answers here:
Split array into chunks
(73 answers)
Closed 4 years ago.
Is it possible to use a higher order javascript function to resize single dimension array into a multi-dimensional array (i.e. 1x6 to 2x3) or do i have to work around with for loops?
Do you have any code examples to resize [0,1,2,3,4,5] to [[0,1,2],[3,4,5]]?
For a real 2D array, you could flat it and take the values with a generator by using new arrays' mapping.
function resize(array, i, j) {
var gen = array.reduce((a, b) => a.concat(b))[Symbol.iterator]();
return Array.from({ length: i }, _ => Array.from({ length: j }, _ => gen.next().value));
}
console.log(resize([[0, 1, 2, 3, 4, 5]], 2, 3));
Using Ramda https://ramdajs.com/docs/#splitEvery
R.splitEvery(3, [0,1,2,3,4,5]); //=> [[0,1,2],[3,4,5]]
Related
This question already has answers here:
Javascript equivalent of Python's zip function
(24 answers)
Zip arrays in JavaScript?
(5 answers)
Transposing a 2D-array in JavaScript
(25 answers)
Closed 1 year ago.
Hi guys I have this array :
[[2,3,0],[0,4,5]]
and i want traverse this array like:
[[2,0],
[3,4],
[0,5]]
Any recommendations please? I am working with javascript
You can easily achieve this result using the Array.prototype.map
const arr = [
[2, 3, 0],
[0, 4, 5],
];
const result = arr[0].map((val, i) => {
return Array(arr.length)
.fill("")
.map((_, index) => arr[index][i]);
});
console.log(result);
This question already has answers here:
Merge keys array and values array into an object in JavaScript
(14 answers)
merge two arrays (keys and values) into an object [duplicate]
(3 answers)
Closed 2 years ago.
Platform: ReactJS
I am trying to combine the two arrays as a date:value object. Any thoughts?
a=["1/1/2020", "1/2/2020", "1/3/2020", "1/4/2020"]
b = [ 1, 2, 3, 4 ]
I am looking for the following result:
["1/1/2020":1, "1/2/2020":2, "1/3/2020":3, "1/4/2020":4]
Thank you,
You can try this approach.
const a=["1/1/2020", "1/2/2020", "1/3/2020", "1/4/2020"]
const b = [ 1, 2, 3, 4 ]
const c = {};
a.forEach((v, i) => {
c[v] = b[i]
});
console.log(c);
This question already has answers here:
Does JavaScript have a method like "range()" to generate a range within the supplied bounds?
(88 answers)
Closed 4 years ago.
Is it any way to create an array a selected size(for example 10) and immediately on the spot fill in it with some numbers specified pattern (1, 2, 3, OR 2, 4, 6 and so on) in one statement in JS?
let arr = new Array(10).fill( (a) => {a = 1; return a++; } );
console.log(arr); // Should be 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
In other words can I pass a some patern or function to method fill insted of a single value.
Actually there's much nicer idiom to create ranges: a spreaded Array constructor:
[...Array(n)]
creates an array of n undefined values. To get 0..n-1, just pick its keys:
[...Array(n).keys()]
for arbitrary ranges, feed it to map:
r = [...Array(10)].map((_, i) => i + 1)
console.log(r)
see here: Does JavaScript have a method like "range()" to generate an array based on supplied bounds?
old answer:
yep, you're looking for Array.from:
r = Array.from({length: 10}, (_, i) => i + 1);
console.log(r)
How does this work? .from inspects its first argument (which can be any object) and picks its .length property. Then, it iterates from 0 to length - 1 and invokes the second argument with two parameters - a value (which is undefined unless the first argument contains a corresponding numeric property) and an index, which takes values from 0...length-1. In this example, we have a function that just returns an index + 1, thus giving us 1..length.
Here's a useful wrapper for the above:
let range = (from, to) => Array.from({length: to - from + 1}, _ => from++);
console.log(range(1, 10))
For ES5, the idiom is Array.apply(null, {length:N}):
r = Array.apply(null, {length: 10}).map(function(_, i) {
return i + 1
})
This question already has answers here:
Sum all integers in a multidimensional array javascript
(9 answers)
Closed 5 years ago.
I'm using a nested array like this:
const data = [
[0],
[2],
[[1], 3]
1
]
Is it possible to count all values together. In this example the result should be 7 (0+2+1+3+1).
And is it also possible to count how many arrays are used? This would be 5 arrays
const sumUp = array => array.reduce((sum, el) => sum + (Array.isArray(el) ? sumUp(el) : +el), 0);
This uses a recursive approach with reduce.
This question already has answers here:
Summing ; delimited values in Javascript
(1 answer)
How to sum elements at the same index in array of arrays into a single array?
(7 answers)
Closed 5 years ago.
I am trying to add together an undetermined amount of arrays, but only add or reduce them item by item.
I cannot figure out how to make this work in ES6 javascript.
const arrayList = [
[1, 2, 3],
[1, 2, 3],
[2, 2, 2]
];
const totalRow = arrayList.map((array) => {
const total = [];
array.map((number, i) => {
total[i] = (total[i] == undefined ? 0 : total[i]) + number;
});
return total;
});
//Output is just 3 more arrays with no change, as it seems
//the total variable resets to an empty array with each iteration.
//what i want is this output:
[4, 6, 8]
What am I doing wrong? I tried reduce method as well but came up empty