how to filter array where title is not null in javascript

index.tsx
const arr = [{id: 1, title: "foo"}, {id: 2, title: null}, {id: 3}];

const filteredArr = arr.filter(item => item.title !== null && item.title !== undefined);

console.log(filteredArr); // [{id: 1, title: "foo"}]
212 chars
6 lines

In the above example, we are using the filter method to get only the items where title is not null or undefined. We are achieving this using a callback function, which returns true only when the title is not null or undefined. The resulting array will only contain the items where the title is not null.

gistlibby LogSnag