Hi Friends 👋,
Welcome To Infinitbility! ❤️
filter() to iterate the array and return a new array based on condition, and
includes() to check string is available in an array or not.
filter()method creates a new array with all elements that pass the test implemented by the provided function.
The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
Okay, let’s plan what we are going to do in code.
- Create sample array variables.
- To get common elements in new array use the
- Use the
includes()method to check string is available in arrays
1// Create sample arrays variables.2const domainsArr1 = ["infinitbility", "aGuideHub", "SortoutCode"];3const domainsArr2 = ["notebility", "aGuideHub", "repairbility"];45// To get common elements in new array use `filter()` method6// Use `includes()` method to check string is available in array7const filteredArray = domainsArr1.filter(value => domainsArr2.includes(value));89console.log("filteredArray", filteredArray)10// Expected 'filteredArray' [ 'aGuideHub' ]
In the above example I used
includes() both methods to create a new array of matched elements.
Here, as per expectaion it should show
'filteredArray' [ 'aGuideHub' ].
let’s check the output.
I hope it’s help you, All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.