I need to filter an array inside the object and return a new array that matches the filter
const example = [ { id: 1, b: [1, 2]}, {id:2, b:[1]}, {id 3, b: [1,3]}
const filter = 1
the return of the filter should just be [{id: 2, b:[1]}]
1) If you only want an object that only contain 1 inside it. then you can it using filter do as:
o.b.length === 1 && o.b[0] === filter
const example = [
{ id: 1, b: [1, 2] },
{ id: 2, b: [1] },
{ id: 3, b: [1, 3] },
];
const filter = 1;
const result = example.filter((o) => o.b.length === 1 && o.b[0] === filter);
console.log(result);
const example = [
{ id: 1, b: [1, 2] },
{ id: 2, b: [1] },
{ id: 3, b: [1, 3] },
];
const filter = 1;
const result = example.filter((o) => o.b.length === 1 && o.b.includes(filter));
console.log(result);