赞
踩
这篇文章中继续介绍在Node-RED中数组元素的定义、拆分、合并与显示等常见操作。
以容器方式启动Node-RED服务,启动命令如下所示:
启动命令:docker run -it -p 1880:1880 -v $PWD/data:/data -e TZ=Asia/Shanghai --name nodered -d nodered/node-red:1.0.4
使用Inject的节点,其中提供JSON格式的数组定义方式,如下图所示
Node-RED还提供了一个简单的JSON编辑器,可以在其中直接输入
选择format JSON即可进行格式化
也可以选择可视化的编辑方式
连接一个Debug类型的节点,部署之后,手动点击触发Inject节点,即可直接进行数组结果显示了
创建一个缺省的split的节点用于拆分数组
连接、部署执行之后结果如下所示
对于数组拆分出来的数字进行四舍五入运算生成去掉小数,可以使用round节点进行处理
连接、部署、执行结果之后,如下图所示,可以看到已经进行了四舍五入了
将分别处理之后的单个元素重新合并,可以使用join节点,此处进行缺省设定即可
连接、部署、执行结果之后,如下图所示,可以看到已经将数组进行合并输出结果了
[{"id":"bff77d9a.20b6c","type":"inject","z":"73554227.4474dc","name":"数字数组输入","topic":"","payload":"[1,2.2,3.345,4.456,5.567]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":120,"wires":[["1e13c184.78c3be"]]},{"id":"b4c533a9.3a2e3","type":"debug","z":"73554227.4474dc","name":"数组结果显示","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":540,"y":220,"wires":[]},{"id":"1e13c184.78c3be","type":"split","z":"73554227.4474dc","name":"拆分数组","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":340,"y":120,"wires":[["8054bdc5.ae709"]]},{"id":"8054bdc5.ae709","type":"range","z":"73554227.4474dc","minin":"0","maxin":"100","minout":"0","maxout":"100","action":"scale","round":true,"property":"payload","name":"数字舍五入","x":530,"y":120,"wires":[["71a9d002.49773"]]},{"id":"71a9d002.49773","type":"join","z":"73554227.4474dc","name":"合并数组","mode":"auto","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":340,"y":220,"wires":[["b4c533a9.3a2e3"]]}]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。