Techiio-author
Started by Teresa PortelaSep 14, 2021

Open
Multiple filters for arrays

0 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWES SHARE

I have 3 arrays. I want to return the elements in array1 distinct from array2 and array3. Example:

array1: ["Peter", "James", "JohnDoe", "Jackie"]
array2: ["James", "Dog"]
array3: ["JohnDoe", "Cat"]

Output: ["Peter", "Jackie"]

Here is my code snippet of filtering array2 out of array1.

 const array1, array2, array3;

const result = array1.filter(
element=> !array2.some(ele => element.id === ele.id)
);
// Output: ["Peter", "JohnDoe", "Jackie"]

I can think of creating another const for filtering array3, but is there a better/elegant way to attach the filters together?

0 Replies

You must be Logged in to reply
Trending Technologies
15
Software40
DevOps48
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance