Robbert
Robbert

Reputation: 713

How to get item from multiple consecutive arrays

I am having a little difficuly coming up with a solution to my problem.

I am trying to get the item at an index of a collection of arrays. I cannot actually concatenate the arrays, they need to stay seperate.

Normally, to get the 3rd item from an array, you would do:

function getItemFromJustOneArray(index){
  var my_array = [1,2,3,4,5,6];
  return my_array[index];
}

getItemFromJustOneArray(2); // returns 3

However, I have a bunch of arrays (could be any amount of arrays) and these arrays cannot be merged into one.

function getItemFromMultipleArrays(index){

  var array1 = [1,2];
  var array2 = [3,4,5];
  var array3 = [6];

  // I cannot use concat (or similar) to merge the arrays,
  // they need to stay seperate
  // also, could be 3 arrays, but could also be 1, or 5...

  // return 3;

}

getItemFromMultipleArrays(2); // SHOULD RETURN 3

I have tried a bunch of lines that loops over the array, but I cannot really get a working solution.

Does someone know an elegant solution to this problem?

Upvotes: 0

Views: 165

Answers (3)

Liad
Liad

Reputation: 844

this should do it, just copying all the arrays into a "big" one and accessing it (added a helping function)

// this function takes any amount of arrays and returns a new
// "concatted" array without affecting the original ones.
function connectArrays(...arrays) {
  return [...arrays.flat()];
}

function getItemFromMultipleArrays(index) {
  var array1 = [1,2];
  var array2 = [3,4,5];
  var array3 = [6];
  var allArrays = connectArrays(array1, array2, array3);
  
  return allArrays[index];
}

getItemFromMultipleArrays(2); // SHOULD RETURN 3

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386654

Why not spread the arrays to a new one and use the index for the value?

function getItemFromMultipleArrays(index) {
    const
        array1 = [1, 2],
        array2 = [3, 4, 5],
        array3 = [6];

    return [...array1, ...array2, ...array3][index];
}

console.log(getItemFromMultipleArrays(2)); // 3

Another approach by using an offset for iterating arrays.

function getItemFromMultipleArrays(index) {
    const
        array1 = [1, 2],
        array2 = [3, 4, 5],
        array3 = [6],
        temp = [array1, array2, array3];

    let j = 0;

    while (index >= temp[j].length) index -= temp[j++].length;

    return temp[j][index];
}

console.log(getItemFromMultipleArrays(2)); // 3
console.log(getItemFromMultipleArrays(5)); // 6

Upvotes: 1

Barmar
Barmar

Reputation: 781235

Nest all the arrays in another array. Then loop over that array, decrementing index by each array's length until it's within the length of the current element. Then you can return the appropriate element of that nested array.

function getItemFromMultipleArrays(index) {

  var array1 = [1, 2];
  var array2 = [3, 4, 5];
  var array3 = [6];
  var all_arrays = [array1, array2, array3];
  var i;
  for (i = 0; i < all_arrays.length && index >= all_arrays[i].length; i++) {
    index -= all_arrays[i].length;
  }
  if (i < all_arrays.length) {
    return all_arrays[i][index];
  }
}

console.log(getItemFromMultipleArrays(2)); // SHOULD RETURN 3

Upvotes: 1

Related Questions