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
// Create sample arrays variables. const domainsArr1 = ["infinitbility", "aGuideHub", "SortoutCode"]; const domainsArr2 = ["notebility", "aGuideHub", "repairbility"]; // To get common elements in new array use `filter()` method // Use `includes()` method to check string is available in array const filteredArray = domainsArr1.filter(value => domainsArr2.includes(value)); console.log("filteredArray", filteredArray) // 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 👍.