Help

Re: Filtered Array by another Array help

907 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Tom_Watson
4 - Data Explorer
4 - Data Explorer

Hey there,

Looking for a bit of help with something that is driving me round the bend!

I’ve wrote a script that basically checks staff availability against a general schedule(i.e. days and times of the week they are generally available) , but also checks to see if they have already been assigned on an exact date. I want to then return only available staff. Currently I have two arrays with the names (strings) of the staff who are generally available and then names of those staff who are already booked that day.

I’d now like to filter one array(avaliablestaff) by the other (unavailable) and be left with only staff who are definitely available. But when I try something like

const filteredlist = availablestaff.filter(name => (!name.includes(unavailable));

Nothing is removed. I put a specific string in there instead (as you can see in the screenshot) and could filter it, but can’t get it to filter by the other array

I’m CLEARLY missing something. I suspect it’s something to do with only having strings, which is because I’m pulling from some lookup fields, but I’ve gone round and round and am baffled. Any help very much appreciated!

script nightmare

2 Replies 2

You’re not technically missing anything. To use a metaphor, what’s wrong here is that you have your puzzle pieces reversed.

The includes method is designed to (long story short) look inside one thing for another thing. In your case, the thing to look inside is the unavailable array, and the thing to search for is each name from the availableStaff array. Your syntax, though, is actually looking inside each name for the full contents of the unavailable array, which will never work.

Fixing this is simple matter of reversing the variable positions in line 115 so that you have this:

const filteredlisttest = availablestaff.filter(name => !unavailable.includes(name))

That will give you a list of all individuals in availablestaff who are not included in unavailable.

Jhon_Albert
4 - Data Explorer
4 - Data Explorer
const arr1 = [4, 23, 7, 6, 3, 6, 4, 3, 56, 4];
const arr2 = [4, 56, 23];
const filterArray = (arr1, arr2) => {
   const filtered = arr1.filter(el => {
      return arr2.indexOf(el) === -1;
   });
   return filtered;
};
console.log(filterArray(arr1, arr2));