当前位置:   article > 正文

Node-RED使用指南:17:数组常见操作_node-red计算

node-red计算

在这里插入图片描述
这篇文章中继续介绍在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节点,此处进行缺省设定即可
在这里插入图片描述
连接、部署、执行结果之后,如下图所示,可以看到已经将数组进行合并输出结果了
在这里插入图片描述

JSON格式的flow

[{"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"]]}]
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/218080
推荐阅读
相关标签
  

闽ICP备14008679号