Get data from array, only when is_wired is true

Hi,

I want to extract certain information from an array
I get the connection data of my devices from
node-red-contrib-unifi / ClientDevices
This connection data is send in an array.
I use node-red-contrib-splitter to split the array, no I have about 26 array elements.
Here is an example of 2 array elements.

"_msgid": "6b6a97a1.12b788",
	"payload": [
		{
			"site_id": "some_data",
			"assoc_time": 1597038040,
			"latest_assoc_time": 1597524359,
			"oui": "Logitech",
			"user_id": "some_data",
			"_id": "some_data",
			"mac": "some_data",
			"is_guest": false,
			"first_seen": some_data,
			"last_seen": some_data,
			"is_wired": false,
			"hostname": "HarmonyHub",
			"use_fixedip": true,
			"network_id": "some_data",
			"fixed_ip": "192.168.0.153",
			"noted": false,
		},
		{
			
			"is_guest": false,
			"first_seen": 1562066511,
			"last_seen": 1597569302,
			"is_wired": true,
			"use_fixedip": true,
			"network_id": "some_data",
			"fixed_ip": "192.168.0.8",
			"noted": true,
			"usergroup_id": "some_data",
			"name": "Homeseer",
			"fingerprint_override": true,
			
		}
	]
}

Now I want to check the array elemnt and see if "is_wired": true
If this is true then I want to get the value of "hostname": "HarmonyHub"
And copy this in a new array. I want to check this for al array elements.
So I get an array output of al hostnames connected with is_wired : false
Something like :
"payload": [
{"HarmonyHub" , “iphone” , “android phone” , “google home” }
If I have a list of al devices connected with the wifi. I want to send the to
node-red-node-ui-list
to create a list of all devices on the Node-red dashboard.

Now my question how do I extract the correct data from the array using the function node.

There’s a great page in the docs that will explain how to use the debug panel to find the right path to any data item.

https://nodered.org/docs/user-guide/messages

Your objects in the array should not end with a comma

why not use the core split node? then process each array element and if it is one where you want the data, put the data into a flow variable array. When the last msg comes thru, then get the data brom the flow variable and send it to the ui_list.

The comma is ther because i removed some data of the array.
i have use splitter and have al the data in seperate messages.
but i thought it would be cleaner to use something a funtion .
with something like

array.forEach(function (where is_wired = false) do
msg.payload = msg.payload + hostname + ","

Look at array filter function.

Something like...

var filtered = msg.payload.filter(e => e.is_wired === true)
msg.payload = filtered

That will return an array of items that are is_wired == true

Note, no spitting necessary, the payload is an array, just filter it.

update...

[{"id":"a31f7aa.4de2b88","type":"function","z":"486d43a.6a75dbc","name":"filter is_wired","func":"var filtered = msg.payload.filter(e => e.is_wired === true)\nmsg.payload = filtered\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":870,"y":160,"wires":[["441a8c64.a3ad54","1e46c4ce.30b14b"]]},{"id":"9ebcb1a0.345ab","type":"inject","z":"486d43a.6a75dbc","name":"faking your data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"site_id\":\"some_data\",\"assoc_time\":1597038040,\"latest_assoc_time\":1597524359,\"oui\":\"Logitech\",\"user_id\":\"some_data\",\"_id\":\"some_data\",\"mac\":\"some_data\",\"is_guest\":false,\"first_seen\":\"some_data\",\"last_seen\":\"some_data\",\"is_wired\":false,\"hostname\":\"HarmonyHub1\",\"use_fixedip\":true,\"network_id\":\"some_data\",\"fixed_ip\":\"192.168.0.153\",\"noted\":false},{\"site_id\":\"some_data\",\"assoc_time\":1597038040,\"latest_assoc_time\":1597524359,\"oui\":\"Logitech\",\"user_id\":\"some_data\",\"_id\":\"some_data\",\"mac\":\"some_data\",\"is_guest\":false,\"first_seen\":\"some_data\",\"last_seen\":\"some_data\",\"is_wired\":true,\"hostname\":\"HarmonyHub2\",\"use_fixedip\":true,\"network_id\":\"some_data\",\"fixed_ip\":\"192.168.0.153\",\"noted\":false},{\"is_guest\":false,\"first_seen\":1562066511,\"last_seen\":1597569302,\"is_wired\":true,\"use_fixedip\":true,\"network_id\":\"some_data\",\"fixed_ip\":\"192.168.0.8\",\"noted\":true,\"usergroup_id\":\"some_data\",\"name\":\"Homeseer\",\"fingerprint_override\":true,\"hostname\":\"Homeseer1\"},{\"is_guest\":false,\"first_seen\":1562066511,\"last_seen\":1597569302,\"is_wired\":false,\"use_fixedip\":true,\"network_id\":\"some_data\",\"fixed_ip\":\"192.168.0.8\",\"noted\":true,\"usergroup_id\":\"some_data\",\"name\":\"Homeseer\",\"fingerprint_override\":true,\"hostname\":\"Homeseer2\"}]","payloadType":"json","x":640,"y":160,"wires":[["a31f7aa.4de2b88","410b4594.b07b8c"]]},{"id":"441a8c64.a3ad54","type":"debug","z":"486d43a.6a75dbc","name":"Filtered data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1070,"y":160,"wires":[]},{"id":"1e46c4ce.30b14b","type":"debug","z":"486d43a.6a75dbc","name":"first items hostname","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload[0].hostname","targetType":"msg","statusVal":"payload[0].hostname","statusType":"auto","x":1100,"y":220,"wires":[]},{"id":"410b4594.b07b8c","type":"debug","z":"486d43a.6a75dbc","name":"All data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":860,"y":100,"wires":[]}]

Thanks i give it a try.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.