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]);


正如您看到的最后注释行所示,它说“ mon”是未定义的,但是如何获取它以显示数字?控制台正常显示为

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


我如何只显示星期一为“ 2”

最佳答案

您可以:

1)将mon放在引号中:

meal_qty[label]["mon"]


2)使用点表示法:

meal_qty[label].mon


Mon不是变量,因此它必须是上述两个变量之一。

关于javascript - JavaScript中的数组处理,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44072707/

10-13 03:44