Filter an array based on another array/list?

Been trying to filter an array based off another list/array. So basically a “contains” piece of functionality.
Tried this node but so far no luck:

Example: My full list/array (with additional data for each thing such as name and some other stuff) is “A”, “B”, “C”, “D”, “E”, “F”, “G” and for a specific selected case by the user I have my filter as “B”, “E”, “F” I want to filter the list to only show the ones included in the filter-list.

Help appreciated.

Couldn’t find a visual node to do this so I built it myself with a javascript function using array.each and array.find.

Hi Anders. I was looking into your issue, but now I see that you’ve beaten me to the solution (again, hehe). I was looking at how to do it in vanilla JS, but it seems your solution might be better as you are using methods associated specifically with Noodl arrays. But really good job! :slight_smile:

In case this questions arises again in the future, would you mind sharing your code/solution in text or screenshots so others can see?

Best regards,
Victor from Noodl

Sure. It’s very specific to the use case in it’s original form but here is a more generic approach.
The approach is this:

  • Walk through each item in the original array
  • Try to find the corresponding item in the filter-array
    – If it’s found, do nothing
    – If not found remove it

Set the resulting array as an output of the function.

var myArray = Noodl.Array.get() // Get me a new array
    //set the array
    myArray.set(Inputs.inData.map((o) => Noodl.Object.create(o)))
    //Walk through the array and check each item if it should be there or not
    myArray.each((object, index) => {
        const filteritem = Inputs.data.forms.find((filteritem) => filteritem === object._id)
        if (filteritem) {
            //This one should be there, keep it in the list (i.e. do nothing)
        } else {
            //This one is not in the filter, remove it from the list
            myArray.remove(object)
        }
    })
    Outputs.filteredArray = myArray
1 Like

Thanks a lot Anders! Great job! It’s okay that it’s a bit specific to your use case, it might still lead other Noodlers towards similar solutions in the future :slight_smile: