traversing a multiarray with Javascript [duplicate] - javascript

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);

Related

Why is this map empty after array.reduce [duplicate]

This question already has answers here:
How do you JSON.stringify an ES6 Map?
(16 answers)
How to serialize a Map in javascript?
(3 answers)
Closed last year.
Why does this map print empty? I'd expect it to be populated from the array? I'm probably missing something small.
let numbers = [1, 2, 3];
let numberMap = numbers.reduce((map, obj) => {
return map.set(obj, obj + 1);
}, new Map());
console.log(JSON.stringify(numberMap));

ReactJs Combine Key and Value [duplicate]

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);

How to find an array in another array? [duplicate]

This question already has answers here:
How to check if an array contains another array?
(6 answers)
javascript search array of arrays
(12 answers)
How to compare arrays in JavaScript?
(55 answers)
Closed 3 years ago.
These codes don't work:
const a = [[1, 1], [2, 2]]
console.log(a.includes([1, 1])); // --> false
console.log(a.indexOf([1, 1])); // --> -1
This work but I think its not optimized
console.log(a.map(x => x.toString()).includes([1, 1].toString()));
// --> true
Is there a simpler way ?
const a = [[1, 1], [2, 2]]
var index=a.findIndex(x=>{return JSON.stringify(x)===JSON.stringify([2, 2])})
console.log(`item index : ${index}`);
Assuming this:
var arr = ['a', 'b', 'b'];
you can invoke:
Array.isArray(arr);
will return true if the considered variable is an array, otherwise not.
Once you get it, you can apply it to the external array.

Re-dimensioning javascript arrays (i.e 1x6 => 2x3) [duplicate]

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]]

JS: How to count all values in nested array [duplicate]

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.

Categories