'How do I for check condition in filter iterating on list and checking for multiple condition which are present in array?

I have authors which can change vary in numbers in list (string array). I have to filter check for each author whether that match to author present in annotation list, and return those values which found true.

Maybe I am doing something caz below code isn't work, it only works when I have single value in author list.

 annotList = annotationList
                ? annotationList
                      .map((annotData) => annotData)
                      .filter((val) =>
                          authors.every(
                              (author: string) => author == val.CreatedBy
                          )
                      )
                : [];

Below is sample data for annotationList on which I have mapped.

0:
AnnotationType: 2
BackgroundColor: "#D43F3A"
BorderColor: "Transparent"
ContentType: 2
Created: "2022-05-04T08:05:32.9309207Z"
CreatedBy: "user1"
DocumentReferenceBorderColor: "#27AAE1"
DocumentReferenceHyperlink: ""
DocumentReferencePageNumber: 1
DocumentReferenceRowKey: ""
ETag: {}
FontSize: null
ForeignKey: "12517735231882099159"
IsDeleted: false
IsPrivate: false
LastAccessedTimeStamp: "2022-05-04T08:05:32.9309207Z"
Opacity: 0.5
PageNumber: 1
PartitionKey: "12517602958622267690_84fa763c"
RowKey: "12517506492670690792_c59126b23"
Timestamp: "2022-05-04T08:05:33.3621897+00:00"
Title: "@"
UserRowKey: "12517665550878303852"
[[Prototype]]: Object

1: {AnnotationType: 2, Title: '@', Content: '{"id":"12517506492677435219_8091276e-15d4-4d7e-b5d…e.com","backgroundColor":"#FDFE98","opacity":0.5}', ContentType: 3, UserRowKey: '12517665550878303852_e0e5bad5-9e3e-415e-ae7a-ddcd2a95ee8d', …}

2: {AnnotationType: 2, Title: '@', Content: '{"id":"12...}
.
.
.

I am populating authors list on which annotation list should be filtered from select mui component for multiple values. so if author list have

authors = [user1, user2];

So these many records should be extracted from list.

This authors list depends on how many authors user select, and how many are there to select.



Solution 1:[1]

with array some method, you can also find the elements that are present in the list

const elements = annotationList.filter(author => authors.some(authorData =>
  {

    return authorData === author.CreatedBy
    }));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sunny