简体   繁体   English

创建一个接受时间范围对象输入并返回合并的时间范围对象javascript数组的组件

[英]create a component which accept the input of time range object and return the array of merged time range object javascript

create a component which accept the input of time range object and return the array of merged time range object 创建一个接受时间范围对象输入并返回合并的时间范围对象数组的组件

Input 输入

[
  {"day":2, "sTime":"08:00", "eTime":"09:00"},
  {"day":2, "sTime":"09:00", "eTime":"10:00"},
  {"day":2, "sTime":"09:00", "eTime":"10:00"},
  {"day":2, "sTime":"10:00", "eTime":"11:00"},
  {"day":2, "sTime":"10:30", "eTime":"11:30"},
  {"day":2, "sTime":"10:00", "eTime":"12:00"},
  {"day":2, "sTime":"10:00", "eTime":"11:00"}
]

Output 产量

[
  { "sTime":"08:00", "eTime":"09:00", "mergedSession": [] },
  { "sTime":"09:00", "eTime":"10:00", "mergedSession": [
    { "day":2, "sTime":"09:00", "eTime":"10:00" },
    { "day":2, "sTime":"09:00", "eTime":"10:00" }
  ]},
  { "sTime":"10:00", "eTime":"12:00", "mergedSession": [
    { "day":2, "sTime":"10:00", "eTime":"11:00" },
    { "day":2, "sTime":"10:30", "eTime":"11:30" },
    { "day":2, "sTime":"10:00", "eTime":"12:00" },
    { "day":2, "sTime":"10:00", "eTime":"11:00" }
  ]}
]

Here's an attempt that produces your desired result. 这是产生您想要的结果的尝试。 What it roughly does (comments in the code should explain some more details) 它的大致作用(代码中的注释应解释更多详细信息)

  1. Prepare the data for easier comparison (add Number values for the times) 准备数据以便于比较(添加时间的Number值)
  2. Go over the data and create sessions 遍历数据并创建会话
  3. If the start of a slot fits in the current session 如果广告位的开头适合当前会话
    • push it to the inner array 推入内部数组
    • extend end of session if needed 如果需要,延长会话结束时间
  4. If the start is outside the slot, create a new session and continue 如果起点在插槽之外,请创建一个新会话并继续
  5. Sanitize the output and put it in the correct order 清理输出并按正确顺序排列

PS Code style is heavily opinionated, let me know if there's stuff that's hard to read... 😅 PS Code风格自以为是,请让我知道是否存在难以阅读的内容...😅

 // UTILS // Utility to go from time string to comparable int const parseTime = str => Number(str.replace(":", "")); // Translate back to a time strin. Eg: 800 -> "08:00" const timeToStr = int => { const str = (int + "").padStart(4, "0"); return [str[0], str[1], ":", str[2], str[3]].join(""); }; // Logic for checking if a slot is in the previous session const inRange = (min, max, t) => t >= min && t < max; // MERGE LOGIC const merge = ([lastMerge, ...merges], { day, sTime, eTime, sVal, eVal }) => // There has to be a "session", lastMerge && // The session has to match our current day, lastMerge.day === day && // Our start has to be within the range of the session: inRange( lastMerge.sVal, lastMerge.eVal, sVal ) ? [{ day, sVal: lastMerge.sVal, // Extend the session if needed eVal: Math.max(eVal, lastMerge.eVal), sTime: lastMerge.sTime, // Update the original string as well, eTime: timeToStr(Math.max(eVal, lastMerge.eVal)), // Add the current slot to the session mergedSession: [ { day, sTime, eTime, sVal, eVal} ].concat(lastMerge.mergedSession) }].concat(merges) : // Create a new session [{ day, sVal, eVal, sTime, eTime, mergedSession: [ { day, sTime, eTime, sVal, eVal} ] }].concat(lastMerge || []).concat(merges); const input = [ {"day":2, "sTime":"08:00", "eTime":"09:00"}, {"day":2, "sTime":"09:00", "eTime":"10:00"}, {"day":2, "sTime":"09:00", "eTime":"10:00"}, {"day":2, "sTime":"10:00", "eTime":"11:00"}, {"day":2, "sTime":"10:30", "eTime":"11:30"}, {"day":2, "sTime":"10:00", "eTime":"12:00"}, {"day":2, "sTime":"10:00", "eTime":"11:00"} ]; console.log( input // Add some numeric "time" values for easier comparisons .map(slot => Object.assign({}, slot, { sVal: parseTime(slot.sTime), eVal: parseTime(slot.eTime) })) // The actual merge .reduce(merge, []) // We concatenated the results in reverse; undo that .reverse() // Remove the temporary int. values used for easier comparisons .map(({ day, sTime, eTime, mergedSession }) => // Remove mergedSessions with 1 element ({ sTime, eTime, mergedSession: mergedSession.length > 1 ? mergedSession.map(({ day, sTime, eTime }) => ({ day, sTime, eTime })) : [] }) ) ) 

var myArray=[{"day":2,"sTime":"08:00","eTime":"09:00"},{"day":2,"sTime":"09:00","eTime":"10:00"},{"day":2,"sTime":"09:00","eTime":"10:00"},{"day":2,"sTime":"10:00","eTime":"11:00"},{"day":2,"sTime":"10:30","eTime":"11:30"},{"day":2,"sTime":"10:00","eTime":"12:00"},{"day":2,"sTime":"10:00","eTime":"11:00"}]; var myArray = [{“ day”:2,“ sTime”:“ 08:00”,“ eTime”:“ 09:00”},{“ day”:2,“ sTime”:“ 09:00”,“ ETIME “:” 10:00 “},{” 天 “:2”,STIME “:” 09:00" , “ETIME”: “10:00”},{ “天”:2 “STIME”:” 10:00" , “ETIME”: “11:00”},{ “天”:2, “表准时”: “10:30”, “ETIME”: “11:30”},{ “天”:2 “STIME”: “10:00”, “ETIME”: “12:00”},{ “天”:2 “STIME”: “10:00”, “ETIME”: “11:00”}] ;

var newarr=[];
var output=[];
var scipArray=[];

function merge(array){ 函数merge(array){

                if(array.length==0)
                {
                        console.log("Array is empty");
                }else{
                    for (var i = 0; i < array.length; i++) {
                    var a=array[i]["sTime"].split(":");
                    var b=array[i]["eTime"].split(":");
                    var aminutes=(+a[0]) * 60 + (+a[1]);
                    var bminutes = (+b[0]) * 60 + (+b[1]);

                    calculateMerge(array,aminutes,bminutes);

                        if(scipArray.includes(i)==true)
                        {
                            //continue;
                        }else{
                    var mergeArray={"sTime":array[i]["sTime"],"eTime":array[i]["eTime"],"mergeSession":newarr};
                    output=output.push(mergeArray);
                        }

                    }console.log(output);
                }   

} }

function calculateMerge(array,aminutes,bminutes){ 函数computeMerge(array,aminutes,bminutes){

        for(var j = 0; j < array.length; j++){

            var c=array[j]["sTime"].split(":");
            var d=array[j]["eTime"].split(":");
            var cminutes=(+c[0]) * 60 + (+c[1]);
            var dminutes = (+d[0]) * 60 + (+d[1]);

            if(aminutes<=cminutes && bminutes>=dminutes){
                newarr.push(array[j]);
                scipArray.push(j);
            }
            else{
                newarr;
            }
        }

} }

merge(myArray); 合并(myArray的);

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

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