Comparing Arrays in NodeRED and generating a list of differences

End Goal: Automatically create or delete folders in the /media/sounds/ share when a new entity is created that qualifies.

To verify if an entity qualifies, I simply add “audio_enabled: true” as an attribute to the entity using the customize.yaml file.

Periodically, NodeRED runs an inject through a flow that assembles various lists of information.

List 1: An array of all entities that are sound enabled. (i.e. audio_enabled: true")
List 2: An array of top level folders found inside of /media/sounds/

List 3: this is the list I’m trying to figure out how to make. It needs to be a list of items from list 1 that are NOT matching anything in list 2.

I have a flow that produces the following msg in NodeRED:

object:
  payload: array[2]
  topic: ""
  audio_enabled: array [2]
    0: object
      entity_id: "input_button.whatever"
    1: object
      entity_id: "whatever"
  top_level: array[12]
    [0 ... 9]
      0: "camera"
      1: "grandfather_clock"
      2: etc.

I need to compare “audio_enabled” to “top_level”. If there’s something in “audio_enabled” that isn’t in “top_level”, push that missing value into a new list. With that list I can then use another node to create that new folder.

I’m imagining something like “for every item in audio_enabled.[i].entity_id, check all items in top_level, and if there’s no match, add that item to a third array. If there is a match, move on.”

Any javascript wizards out there that can help me with this? I can work on splitting up the domain from the entity so we end up with “input_button” as a string, but the rest I’m a bit fuzzy on.

msg.payload = msg.audio_enabled.map((i) => i.entity_id.split(".")[1]).filter((i) => !msg.top_level.includes(i));
return msg;

That was amazing. I sorta understand what’s happening. Although, I did need to change the index position on the split to zero because I want to first compare the domain, then the entity name.

So, for example, the first thing that would happen is we would say, “is there a folder for input_button?” If not, use a node to create that folder.

Then, I’m realizing now that I would also need to check the input_button folder to see if there is already an entity folder inside of it for that input button i.e. button_1 from input_button.button_1.

I’m getting there. This was a huge help! Thanks!

I may have to create a for loop to check all sound_enabled input_button entity suffixes to see if they already exist, but only if the domain folder already existed, otherwise, it would obviously not exist yet.

Thanks!