Created
February 7, 2023 15:25
-
-
Save johnmurch/58a9cefae5c01709628cde9275997092 to your computer and use it in GitHub Desktop.
Simple validation
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
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