0
var meal_qty = {}; 
      var label_options = new Array(); 
      var qty_options = new Array(); 


      <?php foreach($label_name as $meal_option_id => $names_of_labels)
    {?>


      var meal_label_qty = <?php echo $meal_option_id; ?>;

      meal_qty[meal_label_qty] = [];
      var item = {
            mon: <?php echo $_SESSION['protiens']['mon'][$meal_option_id]['qty']?> || null,
            tues: <?php echo $_SESSION['protiens']['tues'][$meal_option_id]['qty']?> || null,
            wed: <?php echo $_SESSION['protiens']['wed'][$meal_option_id]['qty']?> || null,
            thur: <?php echo $_SESSION['protiens']['thur'][$meal_option_id]['qty']?> || null,
            fri: <?php echo $_SESSION['protiens']['fri'][$meal_option_id]['qty']?> || null
      }
      meal_qty[meal_label_qty] = item; 
      <?php }?>


      $('.option').on('click', function() {
            $(".tab_options").hide();
            var label = $(this).attr('label_option');
            console.log(meal_qty[label]);
            //$("#qty1").val(meal_qty[label][mon]);  

As you can see the last commented out line, it says "mon" is undefined, however how can I get it to show the number? the console before it shows normally as

Object{mon:2, tues:null , wed:1 , thur: null, fri: null}

How can I show just monday which is "2"

5
  • Not [mon], but .mon as it is a literal property name, not one stored in a variable named mon. Commented May 19, 2017 at 14:33
  • or ["mon"] too ; objects' properties can accessed through indexing in JS Commented May 19, 2017 at 14:33
  • And how can I make the it 0 inside of null for the other variables? @trincot Commented May 19, 2017 at 14:35
  • There is the define of the js array is in global scope? Commented May 19, 2017 at 14:37
  • @sarah, you can find solutions for that easily by searching a bit: val(meal_qty[label].mon || 0). Commented May 19, 2017 at 14:37

1 Answer 1

1

You would either:

1) put mon in quotes:

meal_qty[label]["mon"]

2) use dot notation:

meal_qty[label].mon

Mon is not a variable, so it has to be one of the two above.

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

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.