Sobes.tech
Middle

/** * Type a function that takes an array of objects and a key, * returns an array of objects where this key/property exists **/ const filterArrayByKey = (items, key) => { return items.filter((item) => item[key] !== undefined && item[key] !== null); };

sobes.tech AI

Answer from AI

typescript function filterArrayByKey<T extends object, K extends keyof T>(items: T[], key: K): T[] { return items.filter(item => item[key] !== undefined && item[key] !== null); }


Here, `T` is the type of objects in the array, and `K` is a key that must be one of the keys of object `T`. The function returns an array of objects where the value at the key `key` is neither `undefined` nor `null`. This ensures strict typing and safety when accessing object properties.
/** * Type a function that takes an array of objects… - sobes.tech