filter is the right tool, but includes is not. The entries in tasks are objects, not strings; none of them will match any of the types in tests.
Instead, the simple version uses some to see if any entries in tasks match the type of the activity being tested:
let tests = activities.filter(a => tasks.some(t => t.type == a.type));
Live Example:
const tasks = [
{
name: "test",
type: "one"
},
{
name: "test2",
type: 'two'
}
];
const activities = [
{
name: "test",
type: "one"
},
{
name: "test2",
type: 'two'
},
{
name: "test3",
type: "three"
}
];
let tests = activities.filter(a => tasks.some(t => t.type == a.type));
console.log(tests);
.as-console-wrapper {
max-height: 100% !important;
}
Naturally, that means at least partially re-traversing tasks for each entry in activities. For the examples you've shown, that's absolutely fine; if tasks is hundreds of thousands of entries long or this is done in a tight loop, not so much. :-)
In that situation, you could give yourself a set of known types from tasks in advance, then just test against the set:
const knownTypes = new Set();
for (const task of tasks) {
knownTypes.add(task.type);
}
let tests = activities.filter(a => knownTypes.has(a.type));
Live Example:
const tasks = [
{
name: "test",
type: "one"
},
{
name: "test2",
type: 'two'
}
];
const activities = [
{
name: "test",
type: "one"
},
{
name: "test2",
type: 'two'
},
{
name: "test3",
type: "three"
}
];
const knownTypes = new Set();
for (const task of tasks) {
knownTypes.add(task.type);
}
let tests = activities.filter(a => knownTypes.has(a.type));
console.log(tests);
.as-console-wrapper {
max-height: 100% !important;
}
Alternately, instead of Set you could also use an object for knownTypes, creating it via Object.create(null) on the off-chance any of your types happens to be something with the same name as a property on Object.prototype:
const knownTypes = Object.create(null);
for (const task of tasks) {
knownTypes[task.type] = true;
}
let tests = activities.filter(a => knownTypes[a.type]);
Live Example:
const tasks = [
{
name: "test",
type: "one"
},
{
name: "test2",
type: 'two'
}
];
const activities = [
{
name: "test",
type: "one"
},
{
name: "test2",
type: 'two'
},
{
name: "test3",
type: "three"
}
];
const knownTypes = Object.create(null);
for (const task of tasks) {
knownTypes[task.type] = true;
}
let tests = activities.filter(a => knownTypes[a.type]);
console.log(tests);
.as-console-wrapper {
max-height: 100% !important;
}