在javascript中具有条件的嵌套循环

时间:2017-03-29 12:23:54

标签: javascript loops

我正在开发一个项目,我想创建一个名为full_day[]的数组,在通过time_plan[]后,它与数组day_schedule()的长度完全相等。

这是我的代码:



function day_schedule(){

var time_plan = [{"lesson_start":"08:00","lesson_end":"09:00"},{"lesson_start":"09:00","lesson_end":"10:00"},{"lesson_start":"10:00","lesson_end":"11:00"},{"lesson_start":"11:00","lesson_end":"12:00"},{"lesson_start":"12:00","lesson_end":"13:00"},{"lesson_start":"13:00","lesson_end":"13:30"},{"lesson_start":"13:30","lesson_end":"14:30"}];

var day = [{"subject_name":"Math-1","lesson_start":"08:00","lesson_end":"09:00"},{"subject_name":"Phy","lesson_start":"11:00","lesson_end":"12:00"},{"subject_name":"Engl","lesson_start":"12:00","lesson_end":"13:00"}];

var key1 = 'lesson_start';
var key2 = 'lesson_end';
var full_day = [];


for (var d = 0; d < day.length; d++) {
                var start_time = day[d].lesson_start;
                var end_time = day[d].lesson_end;

                for (var i = 0; i < time_plan.length; i++) {
                        var search_key = time_plan[i][key1] == start_time && time_plan[i][key2] == end_time;
                        if (search_key == true) {
                            document.writeln('day matched == ' + day[d],'<br>');
                            full_day.push(day[d]);
                        } else {
                            full_day.push({ 
                                lesson_start: ' ',
                                lesson_end: ' ',
                                subject_name: ' '
                            });
                        }

                    }


                console.log('full day',full_day.length, full_day);


            }
}

day_schedule();
&#13;
&#13;
&#13;

我期待

的结果
full day 7 [
  {
    "subject_name": "Math-1",
    "lesson_start": "08:00",
    "lesson_end": "09:00"
  },
  {
    "lesson_start": " ",
    "lesson_end": " ",
    "subject_name": " "
  },
  {
    "lesson_start": " ",
    "lesson_end": " ",
    "subject_name": " "
  },
  {
    "subject_name": "Phy",
    "lesson_start": "11:00",
    "lesson_end": "12:00"
  },
  {
    "subject_name": "Engl",
    "lesson_start": "12:00",
    "lesson_end": "13:00"
  },
  {
    "lesson_start": " ",
    "lesson_end": " ",
    "subject_name": " "
  },
  {
    "lesson_start": " ",
    "lesson_end": " ",
    "subject_name": " "
  }
];

但我不知道该怎么做。

3 个答案:

答案 0 :(得分:1)

如果您希望time_plan尺寸与day相匹配,那么您的迭代完全错误。 您在代码中执行的操作是迭代full_day中的每个元素(3个元素),并为这些元素中的每个元素为time_plan中的每个元素添加一个对象element( 7个要素)。 所以你得到的是:

  1. day中的full_day element您向day添加了7个对象(7个元素)
  2. full_day中的Secon element您将另外7个对象添加到day(14个元素)
  3. full_day中的第三个function day_schedule(){ var time_plan = [{"lesson_start":"08:00","lesson_end":"09:00"}, {"lesson_start":"09:00","lesson_end":"10:00"}, {"lesson_start":"10:00","lesson_end":"11:00"}, {"lesson_start":"11:00","lesson_end":"12:00"}, {"lesson_start":"12:00","lesson_end":"13:00"}, {"lesson_start":"13:00","lesson_end":"13:30"}, {"lesson_start":"13:30","lesson_end":"14:30"}]; var day =[{ "subject_name":"Math1", "lesson_start":"08:00", "lesson_end":"09:00" }, { "subject_name":"Phy", "lesson_start":"11:00", "lesson_end":"12:00" }, { "subject_name":"Engl", "lesson_start":"12:00", "lesson_end":"13:00" }]; var key1 = 'lesson_start'; var key2 = 'lesson_end'; var full_day = []; for (var tp = 0; tp < time_plan.length; tp++) { var start_time = time_plan[tp].lesson_start; var end_time = time_plan[tp].lesson_end; var lesson = { lesson_start: ' ', lesson_end: ' ', subject_name: ' ' }; for (var i = 0; i < day.length; i++) { var search_key = day[i][key1] == start_time && day[i][key2] == end_time; if (search_key == true) { lesson = day[i] } } full_day.push(lesson) } console.log('full day',full_day.length, full_day); } day_schedule();您再次将7个对象添加到Array(21个元素)
  4. 如果你想保留你所使用的逻辑,我建议你反过来转动你的循环:

    &#13;
    &#13;
    find()
    &#13;
    &#13;
    &#13;

    如您所见,这给出了所需的输出。 现在,我们还可以修改代码以使其更漂亮并使用一些有用的JavaScript function day_schedule(){ var time_plan = [{"lesson_start":"08:00","lesson_end":"09:00"}, {"lesson_start":"09:00","lesson_end":"10:00"}, {"lesson_start":"10:00","lesson_end":"11:00"}, {"lesson_start":"11:00","lesson_end":"12:00"}, {"lesson_start":"12:00","lesson_end":"13:00"}, {"lesson_start":"13:00","lesson_end":"13:30"}, {"lesson_start":"13:30","lesson_end":"14:30"} ]; var day =[{ "subject_name":"Math1", "lesson_start":"08:00", "lesson_end":"09:00" }, { "subject_name":"Phy", "lesson_start":"11:00", "lesson_end":"12:00" }, { "subject_name":"Engl", "lesson_start":"12:00", "lesson_end":"13:00" }]; var full_day = []; // Use foreach instead of regular for when you iterate arrays for (tp in time_plan) { var plan = time_plan[tp] var start_time = plan.lesson_start; var end_time = plan.lesson_end; // Use this function to find a suitable lesson given start and end times var lesson = day.find(function(d) { var start_matches = d.lesson_start == start_time; var end_matches = d.lesson_end == end_time; var matches = start_matches && end_matches; return start_matches && end_matches; }) || { // ... or assing a default empty value lesson_start: ' ', lesson_end: ' ', subject_name: ' ' }; full_day.push(lesson) } console.log('full day',full_day.length, full_day); } day_schedule();函数,例如{{1}}

    &#13;
    &#13;
    {{1}}
    &#13;
    &#13;
    &#13;

    希望这有帮助。

答案 1 :(得分:0)

创建另一个函数来解析Object并解析它。

attrgetter(".".join(field.split("__")[:1]), ".".join(field.split("__")[:2]), ".".join(field.split("__")[:3]), ".".join(field.split("__")[:4]))(shipment_instance)

答案 2 :(得分:0)

首先,不要使用document.createElement。这仅适用于页面的第一个渲染,是一个阻塞操作,并且在页面渲染后意外使用将擦除页面。考虑使用DOM操作。 node.appendChildday将是您的朋友。如果可能的话,避免嵌套循环,这是一个坏习惯。如果可能,请使用相邻的循环。

如果您的最终目标是找到time_plan中找到的day中的主题,那么您可以做的是将array.reduce转换为以时间为关键字的对象day。这是使time_plan项唯一的唯一因素,也是唯一可以从array.map识别它的数据。然后使用time_planconst time_plan = [{"lesson_start":"08:00","lesson_end":"09:00"},{"lesson_start":"09:00","lesson_end":"10:00"},{"lesson_start":"10:00","lesson_end":"11:00"},{"lesson_start":"11:00","lesson_end":"12:00"},{"lesson_start":"12:00","lesson_end":"13:00"},{"lesson_start":"13:00","lesson_end":"13:30"},{"lesson_start":"13:30","lesson_end":"14:30"}]; const day = [{"subject_name":"Math-1","lesson_start":"08:00","lesson_end":"09:00"},{"subject_name":"Phy","lesson_start":"11:00","lesson_end":"12:00"},{"subject_name":"Engl","lesson_start":"12:00","lesson_end":"13:00"}]; // Convert day into time-day element pairs // { "08:00-09:00": { "subject_name": "..." }, ... } const dayMap = day.reduce((c, v) => Object.assign(c, {[`${v.lesson_start}-${v.lesson_end}`] : v }), {}); // Convert time_plan to its corresponding day element using time // as key. const full_day = time_plan.map(v => dayMap[`${v.lesson_start}-${v.lesson_end}`]).filter(v => !!v); console.log(full_day);转换为一系列主题。

这只是简单的数组/对象转换,没什么特别的。

array.filter

full_day末尾有一个day,用于删除没有相应{{1}}项的条目。