javascript multidimensional Array get first array last elment,second array second element and 3 array first element
input array=[[1,2,3],[4,5,6],[7,8,9]];
output= [3,5,7];
how to get output with loop in multidimensional array
javascript multidimensional Array get first array last elment,second array second element and 3 array first element
input array=[[1,2,3],[4,5,6],[7,8,9]];
output= [3,5,7];
how to get output with loop in multidimensional array
3
Answers
To get the specific elements from your multidimensional array using a loop in JavaScript, you can access the elements by specifying their indices. Here’s how you can do it:
Explanation:
for
loop.i
):i
is0
, we push the last element of the first array (inputArray[0][inputArray[0].length - 1]
).i
is1
, we push the second element of the second array (inputArray[1][1]
).i
is2
, we push the first element of the third array (inputArray[2][0]
).This gives you the desired output:
[3, 5, 7]
.This answer could definitely be made to be more scalable, but I wasn’t sure if you wanted it to be more dynamic or just to fit this specific case.
You could try to loop with 2 indexes:
You could try to loop with 2 indexes: