PandaMastr
PandaMastr

Reputation: 695

Filter table records based on array of row ids

Hey guys i have a small issue . A little bit of context i have a table with checkboxes . Each row contains an ID when selected i recieve something like this

const mySelectedRoles = [1997,1998,1999]

Once selected i am filtering on my existing data so that i can find a match and send back the ID with the data behind each row to the parent .

setSelectedRowsData(
    tableData.rows.filter(obj=> obj.rowId === mySelectedRoles)
    )

The result that is coming back is an empty Array . Can someone give me a hand ? I will be grateful . Thaks

Upvotes: 0

Views: 690

Answers (2)

duniul
duniul

Reputation: 36

In your code snippet you are comparing the obj.rowId with the whole array instead of checking if it contains the ID. You need to use something like Array.includes instead:

setSelectedRowsData(
    tableData.rows.filter(obj=> mySelectedRoles.includes(obj.rowId))
)

Upvotes: 2

Yevhen Horbunkov
Yevhen Horbunkov

Reputation: 15530

If the point is to filter those table records that have rowId included in mySelectedRoles, it has to be something, like:

setSelectedRowsData(
    tableData.rows.filter(obj=> mySelectedRoles.includes(obj.rowId))
)

Upvotes: 1

Related Questions