'Restrictive filtering array in Javascript/TypeScript for a relationship path problem
I have a filtering problem I am trying to solve in TS/JS and stuck.
I want to filter an array with possible paths with an array of keywords. And it should not have any thing else other than these keywords.
let paths = ['Domain.travelers', 'Domain.travelers.products', 'Domain.travelers.remarks.products', 'Domain.products', 'Domain.vehicle.travelers.products'];
let keywords = ['travelers', 'products'];
When filter is applied, it should only return:
['Domain.travelers', 'Domain.travelers.products', 'Domain.products']
Solution 1:[1]
You can find docs about functions I used here:
const paths = ['Domain.travelers', 'Domain.travelers.products', 'Domain.travelers.remarks.products', 'Domain.products', 'Domain.vehicle.travelers.products'];
const keywords = ['travelers', 'products'];
const results = paths.filter(path =>
path.split('.').slice(1).every(
pathPart => keywords.includes(pathPart)
)
);
console.log(results);
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 | Apolo |
