0

我有一个对象和一个数组。

目的:

{
  "topic": "add",
  "payload": { "id": 1, "end": "24:00", "temp": 21, "add": 0, "delete": 0 },
  "row": -1,
  "socketid": "SSZY-1C3jiP8-NS2AAAB",
  "_msgid": "a891f409.e30038"
}

大批:

[{
  "woche": "Montag",
  "status": 0,
  "_children": [{ "id": 1, "end": "24:00", "temp": 21, "add": 0, "delete": 0 }]
}, {
  "woche": "Dienstag",
  "status": 0,
  "vortag": 1,
  "_children": [{ "id": 2, "end": "24:00", "temp": 21, "add": 0, "delete": 0 }]
}, {
  "woche": "Mittwoch",
  "start": "",
  "status": 0,
  "vortag": 1,
  "_children": [{ "id": 3, "end": "24:00", "temp": 21, "add": 0, "delete": 0 }]
}, {
  "woche": "Donnerstag",
  "status": 0,
  "vortag": 1,
  "_children": [{ "id": 4, "end": "24:00", "temp": 21, "add": 0, "delete": 0 }]
}, {
  "woche": "Freitag",
  "start": "",
  "status": 0,
  "vortag": 1,
  "_children": [{ "id": 5, "end": "24:00", "temp": 21, "add": 0, "delete": 0 }]
}, {
  "woche": "Samstag",
  "start": "",
  "status": 0,
  "vortag": 1,
  "_children": [{ "id": 6, "end": "24:00", "temp": 21, "add": 0, "delete": 0 }]
}, {
  "woche": "Sonntag",
  "start": "",
  "status": 0,
  "vortag": 1,
  "_children": [{ "id": 7, "end": "24:00", "temp": 21, "add": 0, "delete": 0 }]
}]

现在我比较 Id,如果它匹配,我想插入一个新对象。但是现在它在插入后不会中断。有人可以帮助我吗?

var Id = global.get("WochenplanID");
var index = 0;
global.set("table",[{"woche":"Montag","status":0,"_children":[{"id":1,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Dienstag","status":0,"vortag":1,"_children":[{"id":2,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Mittwoch","start":"","status":0,"vortag":1,"_children":[{"id":3,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Donnerstag","status":0,"vortag":1,"_children":[{"id":4,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Freitag","start":"","status":0,"vortag":1,"_children":[{"id":5,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Samstag","start":"","status":0,"vortag":1,"_children":[{"id":6,"end":"24:00","temp":21,"add":0,"delete":0}]},{"woche":"Sonntag","start":"","status":0,"vortag":1,"_children":[{"id":7,"end":"24:00","temp":21,"add":0,"delete":0}]}]
);

var msg = {"topic":"add","payload":{"id":1,"end":"24:00","temp":21,"add":0,"delete":0},"row":-1,"socketid":"SSZY-1C3jiP8-NS2AAAB","_msgid":"a891f409.e30038"};


if (msg.topic == "add") {
    for (var element of global.get("table")) {
        element._children.filter((child) => {
            if (child.id === msg.payload.id) {
                index = element._children.indexOf(child);
                element._children.splice(index, 0, {
                    "id": Id,
                    "end": "",
                    "temp": 21,
                    "add": 0,
                    "delete": 0
                });
            }
        })
    break;
    }
}

msg.payload = global.get("table");
return msg;
4

0 回答 0