简体   繁体   English

动态分配属性和值给javascript对象

[英]Dynamically assign properties and values to a javascript object

I am trying to create a week meal planner. 我正在尝试创建一个星期膳食计划器。 At the moment the scenario is the following: 目前情况如下:

  1. You click on a time of day (breakfast/lunch/dinner) + day of the week; 您单击一天中的某个时间(早餐/午餐/晚餐)+一周中的某天;
  2. A list of recipes fades in; 食谱清单逐渐淡出;
  3. By selecting (clicking) on a recipe you assign this recipe to the day of th week + time of day previously selected. 通过选择(单击)配方,可以将该配方分配给先前选择的星期几+一天中的时间。

I want to store all this data into a JS object, ideally I would like to dynamically create the day object with breakfast/lunch/dinner as keys and recipe as the value but I'm a little stuck here. 我想将所有这些数据存储到一个JS对象中,理想情况下,我想动态地创建一个Day对象,其中将Breakfast / Lunch / dinner作为键,并将配方作为值,但是我对此有些困惑。 I've created a jsfiddle as a little demo of what I'm trying achieve. 我创建了一个jsfiddle,作为我要实现的目标的小演示。 The problem is that when I select for eg recipe-1 for Monday breakfast it does correctly get stored but then, if I select recipe-2 for lunch - breakfast gets reassinged a value of 0. Can someone help me understand why is this happening and guide me to a better approach? 问题是,当我为星期一早餐选择例如食谱1时,它确实可以正确存储,但是,如果我为午餐选择了食谱2,则早餐的价值重新设置为0。有人可以帮助我了解为什么会这样,并且指导我采取更好的方法? Any suggestion/ help is very much appreciated! 任何建议/帮助都非常感谢! Thank you very much! 非常感谢你!

 // find elements var data_day = '', time_of_day = '', recipe = $('.recipes .recipe'), weekly_recipes = { 'week_day': {} }; // show list of recipes $("[data-time]").on("click", function(){ $('.recipes').fadeIn(); time_of_day = $(this).attr('data-time'); data_day = $(this).parents('.column').attr('data-day'); }); recipe.on('click', function(){ var recipe_name = $(this).attr('data-recipe'); weekly_recipes.week_day[data_day] = { 'breakfast': 0, 'lunch': 0, 'dinner': 0 }; $('.recipes').fadeOut(); weekly_recipes.week_day[data_day][time_of_day] = recipe_name; $('.meal-plan').text(JSON.stringify(weekly_recipes)); console.log(weekly_recipes); }); 
 body { background: #20262E; padding: 20px; font-family: Helvetica; } .column{ width: 25%; float: left; padding: 10px; } .column strong{ display: block; margin-bottom: 20px; } .column .wrp{ background: white; } .column [data-time]{ cursor: pointer; margin-bottom: 10px; } .recipes{ width: 100%; display: none; clear: both; margin-top: 40px; background: white; } .recipes span{ display: block; cursor: pointer; margin-top: 10px; } .meal-plan{ margin-top: 20px; background: white; clear: both; margin-top: 40px; background: white; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="column" data-day="monday"> <div class="wrp"> <strong>Monday</strong> <div data-time="breakfast">Breakfast</div> <div data-time="lunch">Lunch</div> <div data-time="dinner">Dinner</div> </div> </div> <div class="column" data-day="tuesday"> <div class="wrp"> <strong>Tuesday</strong> <div data-time="breakfast">Breakfast</div> <div data-time="lunch">Lunch</div> <div data-time="dinner">Dinner</div> </div> </div> <div class="column" data-day="wednesday"> <div class="wrp"> <strong>Wednesday</strong> <div data-time="breakfast">Breakfast</div> <div data-time="lunch">Lunch</div> <div data-time="dinner">Dinner</div> </div> </div> <div class="recipes"> <div class="recipe" data-recipe="recipe-1"> <span data-recipe="recipe-1">recipe 1</span> </div> <div class="recipe" data-recipe="recipe-2"> <span data-recipe="recipe-2">recipe 2</span> </div> </div> <div class="meal-plan"> </div> </div> 

You were almost there but the whole issue was that you were resetting the object to default 0 value everytime the user clicks on the recepie. 您快到了,但是整个问题是您每次用户单击Recepie时都将对象重置为默认0值。

Instead you need to put some check that if it is already initialized then dont reset it to default. 相反,您需要检查一下是否已初始化,然后不要将其重置为默认值。

I have added the below code: 我添加了以下代码:

 if(!weekly_recipes.week_day.hasOwnProperty(data_day) || Object.keys(weekly_recipes.week_day[data_day]).length === 0){
    weekly_recipes.week_day[data_day] = {
      'breakfast': 0,
      'lunch': 0,
      'dinner': 0
    };
  }

See the working code below: 请参见下面的工作代码:

 // find elements var data_day = '', time_of_day = '', recipe = $('.recipes .recipe'), weekly_recipes = { 'week_day': {} }; // show list of recipes $("[data-time]").on("click", function() { $('.recipes').fadeIn(); time_of_day = $(this).attr('data-time'); data_day = $(this).parents('.column').attr('data-day'); }); recipe.on('click', function() { var recipe_name = $(this).attr('data-recipe'); console.log(weekly_recipes.week_day[data_day]); if (!weekly_recipes.week_day.hasOwnProperty(data_day) || Object.keys(weekly_recipes.week_day[data_day]).length === 0) { weekly_recipes.week_day[data_day] = { 'breakfast': 0, 'lunch': 0, 'dinner': 0 }; } $('.recipes').fadeOut(); weekly_recipes.week_day[data_day][time_of_day] = recipe_name; $('.meal-plan').text(JSON.stringify(weekly_recipes)); console.log(weekly_recipes); }); 
 body { background: #20262E; padding: 20px; font-family: Helvetica; } .column { width: 25%; float: left; padding: 10px; } .column strong { display: block; margin-bottom: 20px; } .column .wrp { background: white; } .column [data-time] { cursor: pointer; margin-bottom: 10px; } .recipes { width: 100%; display: none; clear: both; margin-top: 40px; background: white; } .recipes span { display: block; cursor: pointer; margin-top: 10px; } .meal-plan { margin-top: 20px; background: white; clear: both; margin-top: 40px; background: white; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="column" data-day="monday"> <div class="wrp"> <strong>Monday</strong> <div data-time="breakfast">Breakfast</div> <div data-time="lunch">Lunch</div> <div data-time="dinner">Dinner</div> </div> </div> <div class="column" data-day="tuesday"> <div class="wrp"> <strong>Tuesday</strong> <div data-time="breakfast">Breakfast</div> <div data-time="lunch">Lunch</div> <div data-time="dinner">Dinner</div> </div> </div> <div class="column" data-day="wednesday"> <div class="wrp"> <strong>Wednesday</strong> <div data-time="breakfast">Breakfast</div> <div data-time="lunch">Lunch</div> <div data-time="dinner">Dinner</div> </div> </div> <div class="recipes"> <div class="recipe" data-recipe="recipe-1"> <span data-recipe="recipe-1">recipe 1</span> </div> <div class="recipe" data-recipe="recipe-2"> <span data-recipe="recipe-2">recipe 2</span> </div> </div> <div class="meal-plan"> </div> </div> 

Your code is very near to work, you only need to take care when the object of some day already exists to not create it again. 您的代码即将投入使用,只有当某天的对象已经存在时,您才需要注意不要再次创建它。

See below code, you just create a new day when it doesn't exist, if it already exists, then just add the recipe to the time_of_day of that day 参见下面的代码,您可以在不存在的情况下创建新的一天(如果已经存在),然后将配方添加到该天的time_of_day

 var data_day = '', time_of_day = '', recipe = $('.recipes .recipe'), weekly_recipes = { 'week_day': {} }; $("[data-time]").on("click", function(){ $('.recipes').fadeIn(); time_of_day = $(this).attr('data-time'); data_day = $(this).parents('.column').attr('data-day'); }); recipe.on('click', function(){ var recipe_name = $(this).attr('data-recipe'); //CHECK FOR DAY EXISTANCE if (weekly_recipes.week_day[data_day] == null || !weekly_recipes.week_day.hasOwnProperty(data_day)){ weekly_recipes.week_day[data_day] = { 'breakfast': 0, 'lunch': 0, 'dinner': 0 }; } weekly_recipes.week_day[data_day][time_of_day] = recipe_name; $('.recipes').fadeOut(); $('.meal-plan').text(JSON.stringify(weekly_recipes)); console.clear() console.log(weekly_recipes); }); 
 body { background: #20262E; padding: 20px; font-family: Helvetica; } .column{ width: 25%; float: left; padding: 10px; } .column strong{ display: block; margin-bottom: 20px; } .column .wrp{ background: white; } .column [data-time]{ cursor: pointer; margin-bottom: 10px; } .recipes{ width: 100%; display: none; clear: both; margin-top: 40px; background: white; } .recipes span{ display: block; cursor: pointer; margin-top: 10px; } .meal-plan{ margin-top: 20px; background: white; clear: both; margin-top: 40px; background: white; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="column" data-day="monday"> <div class="wrp"> <strong>Monday</strong> <div data-time="breakfast">Breakfast</div> <div data-time="lunch">Lunch</div> <div data-time="dinner">Dinner</div> </div> </div> <div class="column" data-day="tuesday"> <div class="wrp"> <strong>Tuesday</strong> <div data-time="breakfast">Breakfast</div> <div data-time="lunch">Lunch</div> <div data-time="dinner">Dinner</div> </div> </div> <div class="column" data-day="wednesday"> <div class="wrp"> <strong>Wednesday</strong> <div data-time="breakfast">Breakfast</div> <div data-time="lunch">Lunch</div> <div data-time="dinner">Dinner</div> </div> </div> <div class="recipes"> <div class="recipe" data-recipe="recipe-1"> <span data-recipe="recipe-1">recipe 1</span> </div> <div class="recipe" data-recipe="recipe-2"> <span data-recipe="recipe-2">recipe 2</span> </div> </div> <div class="meal-plan"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM