Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dcranmer's avatar

Test whether property is present in two arrays

I need to find (or filter for) objects in Obj1 that have any items in their filters property (an array) that is also present in the checkedFilters array.

I've tried using some(), but haven't been able to come up with the right combination yet.

Obj1: contains sub-objects structured kike this:

African Cultural Studies:Object
degreeTypes:Array[2]
filters:Array[4]
0:33
1:38
2:39
3:44

checkedFilters:: an array that contains a list of checked filter Ids

checkedFilters:Array[1]
0:35
1:44
0 likes
4 replies
LaryAI's avatar
Level 58

One possible solution is to use the filter() method to iterate over the objects in Obj1 and return only those that have at least one filter that is also present in checkedFilters. Here's an example implementation:

const filteredObj1 = Obj1.filter(obj => {
  return obj.filters.some(filter => checkedFilters.includes(filter));
});

This code creates a new array filteredObj1 that contains only the objects from Obj1 that meet the filter criteria. The filter() method takes a callback function that is called for each object in Obj1. The callback function returns true if the object should be included in the filtered array, and false otherwise.

In this case, the callback function checks if any of the filters in the current object (obj.filters) are present in the checkedFilters array. This is done using the some() method, which returns true if at least one element in the array satisfies the provided testing function. The testing function simply checks if the current filter is included in checkedFilters.

If the some() method returns true, it means that the current object has at least one filter that is also present in checkedFilters, so the callback function returns true and the object is included in the filtered array. If some() returns false, the callback function returns false and the object is excluded from the filtered array.

Note that this implementation assumes that Obj1 and checkedFilters are both arrays of objects/numbers, respectively, as shown in the question description. If the actual data structures are different, the code may need to be adapted accordingly.

dcranmer's avatar

Maybe a human can please help me out here -- the ChatBot was close, but I'm still getting an error)? Here's a sample object in the array I want to filter:

0:Object
degreeTypes:Array[2]
filters:Array[4]
0:33
1:38
2:39
3:44
guideLink:"https://something.somewhere.net"
title:"African Cultural Studies"

If I run:

const filteredObj1 = Obj1.filter(obj => {
  return obj.filters.some(filter => checkedFilters.includes(filter));
});

or

let filteredObj1 = Ob1.filter(obj => obj.filters.some(filter => .checkedFilters.includes(filter)));

I get Uncaught TypeError: Cannot read properties of undefined (reading 'some')

gastond's avatar
gastond
Best Answer
Level 1

try

const filteredObj1 = Obj1.filter(obj => obj?.filters?.some(filter => checkedFilters.includes(filter)) ?? false);

dcranmer's avatar

Thanks @gastond . In doing further testing, that's exactly what I came up with to get rid of the error -- and I needed to make one other change:

the arrays being checked need to be reversed so that the target array comes first. As in

const filteredObj1 = Obj1.filter(obj => checkedFilters.some(filter => obj?filters?.includes(filter)) ?? false);

1 like

Please or to participate in this conversation.