0

我有一个 HTML 表单,用户可以在其中向表单添加时间数据(开始和结束),并且可以选择多天来保存它。(这是一个循环泵每天的开关时间)。

从用户的角度来看,表单看起来像这样

这就是我处理表格的方式:

function saveCircularPumpTimes(){
    $("#circularTableForm").on("submit",function(e){
        e.preventDefault();
        var inputs = $("#circularTableForm").serializeArray();
        console.log(inputs);
        /*
        var newCircularObj = {
                "mon":[
                    { start:"", end:"" },
                    { start:"", end:"" }
                ],
                "tue":[
                    { start:"", end:"" },
                    { start:"", end:"" }
                ],

        };
        */
        var days = $("#circularPumpDays").val();
        var datas = [];
        for(var i = 0; i < inputs.length; i++){ 
        // get all the time data to an array for easier processing later.
            if( inputs[i].name.includes("circularPumpOnTimeStart") ){
                datas.push( { start:"", end:"" } );
                datas[datas.length-1].start = inputs[i].value;
            }else if( inputs[i].name.includes("circularPumpOnTimeEnd") ){
                datas[datas.length-1].end = inputs[i].value;
            }
        }
        console.log("Days: ",days);
        console.log("Datas: ",datas);
    });
}

days 数组如下所示:

["mon", "tue", "wed"]

数据如下所示:

[
   {start: "15:36", end: "16:36"},
   {start: "15:36", end: "15:36"},
   {start: "15:36", end: "15:37"},
   {start: "15:36", end: "15:37"}
]

当用户选择多天时,我会这样做:

       for(var i = 0; i < days.length; i++){
            if(Object.prototype.hasOwnProperty.call(newCircularObj, days[i])) {
                // User already have saved data for this day
                if( prompt("This day already exist with data inside it. Would you like to overwrite?") ){
                    newCircularObj[days[i]] = [];
                    for(var k = 0; k < datas.length; k++){
                        newCircularObj[days[i]].push( datas[i] );
                    }
                }
            }else{
                // We don't have saved data for this day, create new
                newCircularObj = new Object(days[i]);
                for(var k = 0; k < datas.length; k++){          // yeah this loop is duplicate. :( i will put it into a function
                    newCircularObj[days[i]].push( datas[i] );
                }
            }
        }

这种方法的问题是数据将在对象内部多次存在,我必须将其保存到 MCU 中,因此空间非常重要。例如,如果用户每次都选择相同时间的所有 7 天,则数据将在对象中包含 7 次。

在 MCU 方面,我使用带有 ESP32 的ArduinoJSON库,我会像这样处理它(你可以看到整个字符串化的 json 对象一直在 ram 内,所以我真的需要空间):

static const inline void processData(String DATA){ // in every single minute we have to do this and search for the start time.
    boolean found = false;
    SpiRamJsonDocument doc(15000);
    DeserializationError error = deserializeJson(doc, DATA); // we get the data as a JSON string.
    if (error) {
        String Error = "JSON Error Pump ~ ";
        Error += String(error.c_str());
        errorLog(Error);
        Send_Async("No Memory for circular Pump DATA", ";RoomHeating");
    }else {
        JsonObject documentRoot = doc.as<JsonObject>();
        for (JsonPair timeData : documentRoot) {
            if(Time.Get_Day() == timeData.key().c_str()){ // Time.Get_Day() is equalent with the current day String like "mon" or "tue"
                // if the day is okay we have to search for the times
                JsonArray Day = doc[Time.Get_Day()];
                for(int i = 0; i < Day.size(); i++){
                    if(Time_Has_Reached(Day[i]["start"],Day[i]["end"])){ // function for check if the start time has passed but the end is not
                        // if in between
                        found = true;
                        break;
                    }
                }
                if(found){break;}
            }
        }
    }

    if(found){
        // Turn on the pump
    }else{
        // Turn off the pump
    }
}

所以问题是,如果时间数据已经保存到一天以提高效率,我该如何安排对象以便时间数据只存在一次。

编辑:来自最终对象的示例:

var newCircularObj = {
                "mon":[
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                ],
                "tue":[
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                ],
                "wed":[
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                ],
                "thu":[
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                ],
                "sat":[
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                ],

};
4

1 回答 1

0

好的,这个程序从以下位置删除重复项newCircularObj

我已经取了你的样本并修改了一些值newCircularObj

$(function() {

  function removeDup(ar) {
    return ar.reduce(function(item, e1) {
      var matches = item.filter(function(e2) {
        return e1.start == e2.start && e1.end == e2.end
      });
      if (matches.length == 0) {
        item.push(e1);
      }
      return item;
    }, []);

  }


  var result = {datas:{} };
  Object.keys(newCircularObj).forEach(function(it, i) { 
        result.datas[it] =  removeDup(newCircularObj[it]);
    });


  console.log(result);


});

var newCircularObj = {
                "mon":[
                    { start:"14:10", end:"15:10" },
                    { start:"14:11", end:"15:10" },
                    { start:"14:10", end:"15:11" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                ],
                "tue":[
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:11" },
                    { start:"14:11", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                ],
                "wed":[
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                ],
                "thu":[
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                ],
                "sat":[
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                    { start:"14:10", end:"15:10" },
                ],

};
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


结果

{
  datas: {
    mon: [
    {  end: "15:10",  start: "14:10"},
    {  end: "15:10",  start: "14:11"},
    {  end: "15:11",  start: "14:10"}
    ],
    sat: [
    {  end: "15:10",  start: "14:10"}
    ],
    thu: [
    {  end: "15:10",  start: "14:10"}
    ],
    tue: [
    {  end: "15:10",  start: "14:10"},
    {  end: "15:11",  start: "14:10"},
    {  end: "15:10",  start: "14:11"}
    ],
    wed: [
    {  end: "15:10",  start: "14:10"}
    ]
  }
}
于 2021-03-04T17:40:14.757 回答