How to filter an array based on a property in a different array

repost from Discord

Hi, i tried to adapt Anders function from this page (https://forum.noodl.net/t/filter-an-array-based-on-another-array-list/100) to filter my arrays, but I’m not finding the solution

this is what I try:
I have an array of items from a database table 1. then a second array from database table 2 where table 1 is linked through a pointer.

Now i´d like to find out which items from array 1 are NOT array 2 and make an array 3 out of these “left overs”.

so:
Array1[1,2,3,4,5,6,7]
Array2[1{Array1_id:3}, 2{Array1_id:7}]
Array3[1,2,4,5,6]

Thanks

Here is a small example that uses a function node to do the filtering.

It uses the JavaScript filter() and find() functions on arrays. Here are a few screenshots and I have also just included the code as text at the bottom.

I have also made a small example project that I have shared on our Discord.

This is my node graph. I use two Static Arrays to create the example content:

Here is what the arrays look like:

This is the code in the Function node:

Here is the result in the preview window:

Here is the code as text:

// We use the filter function to go through all the items in array1 
// and if the "filter" (the find function result in our case) is true we add the item into the newArray.
let newArray = Inputs.array1.filter(array1Item => {
   //here we use the find function to compare the id's and we return the 
   //result of that comparison that will either be true or false
   //if array2 has the id of an item in array1 it will return true
   let findResult = Inputs.array2.find(array2Item => {
      //if the array2Item.array1_id is the same as array1Item.id we return true
      return array2Item.array1_id==array1Item.id

   })

   //if the findResult was true we don't want to return the item
   //so here we check if the findResult was false in which case we return the item from array1
   if(!findResult) return array1Item;

})

Outputs.filteredArray=newArray
1 Like

this is plain magic! thank you so much

1 Like