Skip to content

Instantly share code, notes, and snippets.

@johnmurch
Created February 7, 2023 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnmurch/58a9cefae5c01709628cde9275997092 to your computer and use it in GitHub Desktop.
Save johnmurch/58a9cefae5c01709628cde9275997092 to your computer and use it in GitHub Desktop.
Simple validation
const validKeyNames = ['name', 'gender', 'hasTheForce']
var a = { name: 'Luke Skywalker', gender: 'Male', hasTheForce: true }
var b = { name: 'James Brown', gender: 'Male', hasTheFunk: true }
function check(obj, arr) {
return Object.keys(obj).every(e => arr.includes(e));
}
console.log(check(a, validKeyNames))
console.log(check(b, validKeyNames))
// originally posted at https://stackoverflow.com/questions/44240185/validate-javascript-object-keys-against-an-array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment