Checking for multiple keys in a JavaScript object?

Is there a native or library function for checking multiple keys at once?

Say this is my object:

const foo = {};
foo.superLongNameNeededByPackage = {cat: {black: 1}, hat: 2, bat:3, mat: 12}
// { superLongNameNeededByPackage : {cat: {black: 1}, hat: 2, bat:3, mat: 12} }

And I want to check that it has various keys on it, if there a way to do this?

const isReadyForHalloween = hasMultipleKeys(foo.superLongNameNeededByPackage,
  ['', 'hat', 'bat']);

I’m aware of lodash’s has as well as the fact I can write my own using Object.keys and every but I was hoping to avoid it if possible, becuse it would still feel overly verbose and requires writing out the full formulation.

const arr = Object.keys(foo.superLongNameNeededByPackage);
arr.every(item => item.hasOwnProperty("a")
               && item.hasOwnProperty("b")
               && item.hasOwnProperty("c") );

But I don’t want to be writing out hasOwnProperty each time, and may want to supply my key list as its own variable.

36 thoughts on “Checking for multiple keys in a JavaScript object?”