1

I am trying to call an array reference using the name of different array:

My alert keeps coming up as undefined. I'm not sure what Im missing here. I am expecting to see a result of "Water1"

var Water2 = new Object();
Water2.Name = 'Water2';
Water2.Level = '1';
Water2.Common = 'Water1';
Water2.CanBuild = 'no';
Water2.CanWalk = 'no';
Water2.SpawnChance = '5';

$(".cube").each(function(index) {
  var thiscubetype = $(this).attr('class').split(' ')[1];
  var nextcubetype = $(this).next().attr('class').split(' ')[1];
  var nextcommon = nextcubetype.Common;
  console.log(nextcommon);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="cube Water1"></div>
<div class="cube Water2"></div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Dustin
  • 147
  • 1
  • 1
  • 13
  • Use an object containing all possible `Water`s, either indexed by the number or the class name, then use bracket notation to access the appropriate property on the object to get to the inner Water object – CertainPerformance Jun 05 '19 at 05:42
  • I made you a snippet. You have one more issue - `var nextcubetype = $(this).next().attr('class')` will not return anything on the second div because there is no next after the second – mplungjan Jun 05 '19 at 05:44

0 Answers0