return array of array indexes in javascript

To return an array of array indexes in JavaScript, you can use nested loops to iterate through the array and its elements, and then use the push method to add the indexes to a new array.

Here's an example code snippet to achieve this:

index.tsx
function getIndexesOfArray(arr) {
  const indexes = [];
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr[i].length; j++) {
      indexes.push([i, j]); // add the indexes to the array
    }
  }
  return indexes;
}

// Example use case:
const myArray = [[1, 2], [3, 4], [5, 6]];
console.log(getIndexesOfArray(myArray)); // Output: [[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1]]
398 chars
14 lines

In this example, the getIndexesOfArray function takes an array as its parameter and initializes an empty indexes array. The nested for loops iterate through the array and its elements, and the push method adds the indexes of each element to the indexes array. Finally, the function returns the indexes array.

gistlibby LogSnag