Last active
January 31, 2023 21:23
-
-
Save psenger/2794dfe2f7a6a4dc7fd2e6b8ab8647fa to your computer and use it in GitHub Desktop.
[Has Any Listed Attributes] #JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| Consider that this does not look at prototype, so will have issues with anything that is a class or has a prototype | |
| OR better | |
| https://lodash.com/docs/4.17.15#has | |
| **/ | |
| const hasAnyAttrs = (obj, attrs) => { | |
| if (obj && !!attrs) { | |
| return (attrs||[]).map(attr => obj[attr] !== undefined).filter(_ => _).length !== 0 | |
| } | |
| return false | |
| } | |
| const subject = {msg: 'hello', amt: 100, isMaster: false}; | |
| [ | |
| null, // When subject is null hasAnyProperties= false | |
| [], // When subject is hasAnyProperties= false | |
| ['msg'], // When subject is msg hasAnyProperties= true | |
| ['msg', 'isMaster'] // When subject is msg,isMaster hasAnyProperties= true | |
| ] | |
| .forEach((attrs) => { | |
| console.log(`When subject is ${attrs} hasAnyProperties=`, hasAnyAttrs(subject, attrs)) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment