0

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?

6
  • 3
    If there can be an arbitrary number of arrays, it should be a 2-dimensional array, not separate variables for each. Commented Jun 3, 2021 at 16:08
  • 1
    Why can't you merge them? Commented Jun 3, 2021 at 16:08
  • 2
    Can you have a temporal array to merge all the arrays? Commented Jun 3, 2021 at 16:08
  • On what basis you want the return value to be 3? What if you have a nested array or an associative array or a multi-dimensional array? Commented Jun 3, 2021 at 16:10
  • the rationale for why you want to do this is missing, but if you want a solution you need to check the index against the length of the array(s) to find which array it is in and adjust the index based on skipping arrays. e.g. if (index < array1.length) return array1[index]; else if (index - array1.length < array2.length) return array2[index - array1.length] else return array3[index - array1.length - array2.length] or something like that... Commented Jun 3, 2021 at 16:13

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

could do this without the nesting too and more explicit coding agaist the arrays. this solution works better if there is an arbitrary number of arrays. Explicitly coding checks against arrays might be better if there is a limited number of explicit arrays
The question say "could be 3 arrays, but could also be 1, or 5...". So it seems like it needs to work for an arbitrary number.
Thank you Barmar, your answer has been very helpful! And yes, it needed to work with an arbitrary amount. Thanks again :-)
1

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

3 Comments

He wrote "these arrays cannot be merged into one.". That's what you're doing with the ellipsis.
Maybe this is a coding challenge, where creating the combined array will be too large.
Hi, your second anwser was helpful to me! Thanks a lot. If I could mark two correct answers, I would. (It's just that I prefer for-loops, that's all). Thanks again!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.