在JS中增加数组

时间:2018-02-12 11:03:49

标签: javascript

我正在尝试列出最新的消息列表。这是Node红色。我有一个4行LCD我希望在

上显示寻呼机信息

对我来说,这应该有效,但它没有,只是在最后一行显示消息而不会添加新消息

var message = [];

message.splice(4,0,msg.message);
message.splice(0,1);

msg.payload1 = message[0];
msg.payload2 = message[1];
msg.payload3 = message[2];
msg.payload4 = message[3];
return msg;

这是Node red flow

[{"id":"dd82941d.5b65c8","type":"function","z":"4cce1974.6fb398","name":"Format Pager Message","func":"var str = msg.payload;\nvar pager = str.substring(9,16);\nvar length = str.length - 2;\nvar message = str.substring(17,length);\nmsg.pager = pager;\nmsg.message = message;\nreturn msg;","outputs":1,"noerr":0,"x":311,"y":195,"wires":[["2b7dc9b1.c01356"]]},{"id":"188de5a3.ba258a","type":"rpi-lcd","z":"4cce1974.6fb398","name":"","pins":"12,7,8,25,24,23","x":1113,"y":195,"wires":[]},{"id":"f66dd00d.9b67e","type":"template","z":"4cce1974.6fb398","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"1:{{payload1}}\n2:{{payload2}}\n3:{{payload3}}\n4:{{payload4}}","x":942,"y":193,"wires":[["188de5a3.ba258a"]]},{"id":"d6971b25.349b38","type":"debug","z":"4cce1974.6fb398","name":"","active":true,"console":"false","complete":"true","x":717,"y":245,"wires":[]},{"id":"7ee7f56e.176e5c","type":"inject","z":"4cce1974.6fb398","name":"","topic":"test","payload":"0123456789abcdefgHello Worldyz","payloadType":"str","repeat":"","crontab":"","once":false,"x":115,"y":229,"wires":[["dd82941d.5b65c8"]]},{"id":"2b7dc9b1.c01356","type":"function","z":"4cce1974.6fb398","name":"Stack Messages","func":"var message = [];\n\nmessage.splice(4,0,msg.message);\nmessage.splice(0,1);\n\nmsg.payload1 = message[0];\nmsg.payload2 = message[1];\nmsg.payload3 = message[2];\nmsg.payload4 = message[3];\nreturn msg;","outputs":1,"noerr":0,"x":539,"y":194,"wires":[["f66dd00d.9b67e","d6971b25.349b38"]]}]

2 个答案:

答案 0 :(得分:1)

使用unshift将项添加到数组的前面。不要搞乱拼接它。

message.unshift(msg)

使用pop删除数组中的最后一个元素。

while (message.length > 4) {
    message.pop();
}

答案 1 :(得分:-1)

使用全局变量解决

=IF(AND(A1+1<=$B$1,NOT(ISBLANK(A1))), A1+1,)