0

I have a multidimensional array of 100 trucks. Each truck holds several boxes. I insert the first box of each truck by splicing from the box array to the truck array:

removed = allBoxes.splice(x, 1)[0];
trucks[i] = {0: removed};

Because I know it's the first box, I can put the {0:. But to add the next box, I want it to be dynamic. I can insert it at the right place, but the key is undefined instead of 1 for the next box.

removed = allBoxes.splice(x, 1)[0];
trucks[i][trucks[i].length] = removed;

You can see the results here: Instead of undefined:, can it say 1? Whenever I try to add a variable in the array, it just writes out the variable string, not the value.

enter image description here

Alteredorange
  • 556
  • 1
  • 6
  • 23

1 Answers1

0

As @JordanBurnett mentioned, it wasn't an array. I had to count how many objects were in the array. Using the below code (from this question) gave the desired result:

var size = Object.keys(trucks[i]).length;
trucks[i][size] = removed;

enter image description here

Alteredorange
  • 556
  • 1
  • 6
  • 23