Illustration of Dan in a circle

Filter an array by an array

I needed to do something that was out of the realm of the normal docs and examples today, and I got a bit stuck. I had a list of 15 items that I needed to filter down to 3 specific items. I tried a bunch of things, but then luckily, Jared shared a solution.

In hindsight, it's so simple but still doesn't feel intuitive to me. I want to doc it here in case I need it again.

const things = [{ id: 1 }, { id: 2 }, { id: 3 }];
const wantedThings = [1, 2];

const chosen = things.filter(thing => wantedThings.includes(thing.id));

The odd part to me is flipping the arrays. I kept wanting to wriite it in where thing includes a wantedThing. TIL.

Here's pen if you wanna see the console.log.