Skip to content

Instantly share code, notes, and snippets.

@psenger
Last active January 31, 2023 21:23
Show Gist options
  • Select an option

  • Save psenger/2794dfe2f7a6a4dc7fd2e6b8ab8647fa to your computer and use it in GitHub Desktop.

Select an option

Save psenger/2794dfe2f7a6a4dc7fd2e6b8ab8647fa to your computer and use it in GitHub Desktop.
[Has Any Listed Attributes] #JavaScript
/**
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