Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export const objectFilter = ( obj, predicate ) => {
- if ( obj === null || obj === undefined )
- throw Error( 'Cannot convert null or undefined to an object' )
- if ( typeof predicate !== 'function' )
- throw Error( 'Expected predicate to be a function' )
- obj = Object( obj )
- return Object.keys( obj ).reduce(
- ( filtered, key ) => {
- if(
- Object.prototype.hasOwnProperty.call( obj, key ) &&
- predicate( obj[ key ], key, obj )
- ){
- filtered[ key ] = obj[ key ]
- }
- return filtered
- },
- {}
- )
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement