Menu

gitpiper

assertValidKeys javascript Code Snippet in 2024

objectintermediate

Last Updated: 10 October 2024

Validates all keys in an object match the given keys.

  • Use Object.keys() to get the keys of the given object, obj.
  • Use Array.prototype.every() and Array.prototype.includes() to validate that each key in the object is specified in the keys array.
const assertValidKeys = (obj, keys) => Object.keys(obj).every(key => keys.includes(key));
assertValidKeys({ id: 10, name: 'apple' }, ['id', 'name']); // true assertValidKeys({ id: 10, name: 'apple' }, ['id', 'type']); // false

javascript snippet similar to assertValidKeys For You in October 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️