Array.prototype.filter()
to filter the array based on the predicate fn
so that it returns the objects for which the condition returned a truthy value.Array.prototype.map()
to return the new object.Array.prototype.reduce()
to filter out the keys which were not supplied as the keys
argument.const reducedFilter = (data, keys, fn) => data.filter(fn).map(el => keys.reduce((acc, key) => { acc[key] = el[key]; return acc; }, {}) );
const data = [ { id: 1, name: 'john', age: 24 }, { id: 2, name: 'mike', age: 50 } ]; reducedFilter(data, ['id', 'name'], item => item.age > 24); // [{ id: 2, name: 'mike'}]
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️