0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Node-REDで2つの配列を結合する

Posted at

Node-REDで2つの配列を結合する方法がわからなかったのでメモします。
Functionノードを使う場合と、JSONata式を使う場合を記載します。

Functionノードを使う場合

array_1とarray_2を結合したい場合はconcatを使うと2つの配列を結合できます。
例えば、Functionノードに以下のように記載すると結合した結果がmsg.payloadに代入されます。

let array_1 = [1,2,3];
let array_2 = [4,5,6];
msg.payload = array_1.concat(array_2);
return msg;

JSONata式を使う場合

array_1とarray_2を結合したい場合は$appendを使用します。

$append(array_1,array_2)	

例えば、changeノードに以下のように記載すると、結合した結果がpayloadに代入されます。

Screenshot 2021-09-13 22.41.13.png

ノードの例を以下に記載します。

[{"id":"e5a5772ff8540a69","type":"inject","z":"9e623264ef90ad1e","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":250,"y":120,"wires":[["7ca3081dcf96fa12"]]},{"id":"d1b43237f8c4ac75","type":"debug","z":"9e623264ef90ad1e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":670,"y":120,"wires":[]},{"id":"7ca3081dcf96fa12","type":"change","z":"9e623264ef90ad1e","name":"配列の結合","rules":[{"t":"set","p":"array_1","pt":"msg","to":"[1,2,3]","tot":"json"},{"t":"set","p":"array_2","pt":"msg","to":"[4,5,6]","tot":"json"},{"t":"set","p":"payload","pt":"msg","to":"$append(array_1,array_2)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":120,"wires":[["d1b43237f8c4ac75"]]}]

まとめ

配列を結合する方法を2通り紹介しました。
個人的にはFunctionノードをあまり使いたくないため、JSONata式を利用した方法を活用していきたいと考えています。

その他、もっとかんたんな結合方法をご存じの方がいれば教えて下さい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?