-1

I have 2 arrays, arr1 and arr2. They're both 2-dimensional. I want to copy certain array values from arr1 to arr2.

For instance, I want to copy the value from arr1[9][9] into arr2[0][0]. My guess was to write arr2[0][0] = arr1[9][9]; but that failed.

I looked at some similar questions on this site but they did not answer my question.


Here is the code for the particular situation. The code is written is Google Apps script.

  // eplList and attList are both arrays. They are filled below (I checked, the values exist)
  var eplList = epl.getRange(2, 2, eplLastRow, 2).getValues();
  var attList = attsheet.getRange(3, 1, attLastRow, 20).getValues();

  var eplListLength = eplList.filter(String).length;
  var attListLength = attList.filter(String).length;

  // Declaring the empty array I want to fill
  var masterArray = [];

  var ix, jx, day;

  // Here I begin to fill the array
  for (ix = 0; ix < eplListLength; ix++)
  {
    masterArray[ix][0] = eplList[ix][0]; // This is where I am getting the error message 
    masterArray[ix][1] = eplList[ix][1];

      for (jx = 0; jx < attListLength; jx++)
      {
        if (eplList[ix][0] == attList[jx][day*4-4])
          masterArray[ix][6+day] = masterArray[ix][6+day].concat(" ", attList[jx][day*4-2], ": ",attList[jx][day*4-3]);
      };
 // and some morecode
  };

The error I'm getting is "TypeError: Cannot set property '0' of undefined"

10
  • share your array data. Commented Apr 9, 2021 at 8:12
  • Share your arrays along with the error that you're getting Commented Apr 9, 2021 at 8:13
  • updating question. thank you Commented Apr 9, 2021 at 8:14
  • 2
    You need to initialize the inner array: masterArray[ix] = [] in the for loop. Also, the variable day is never set a value Commented Apr 9, 2021 at 8:23
  • 1
    @user256872 where? You need to initialize the each inner array. for (ix = 0; ix < eplListLength; ix++) { masterArray[ix] = []; <-- here Commented Apr 9, 2021 at 8:25

1 Answer 1

1

To fix particular issue you've got try this code, but i not sure about code below):

masterArray[ix] = [eplList[ix][0], eplList[ix][1]]; // This is where I am getting the error message

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

5 Comments

So, you do not need this line at all: masterArray[ix][1] = eplList[ix][1];
No problem, what about code below (nested for). Did it fail?
so far so good, it's still in development :)
@user256872 you need to set the value of day somewhere. It's undefined and 6 + day will return NaN
yes I have it set in my code I just accidentally removed it here. thank you

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.