11

How can I most succinctly check if an object contains ALL of the keys specified in an array?

For example:

var arr = ["foo", "bar"];

var obj = {
  foo: 1,
  bar: "hello"
};

magic_function(arr, obj); // return true, all keys from array exist


var obj2 = {
  foo: 12,
  bar: "hi",
  test: "hey"
};

magic_function(arr, obj2); // return true, all keys from array exist,
                           // keys not specified in array don't matter


var obj3 = {
  foo: 5
};

magic_function(arr, obj3); // return false, "bar" is missing
Emphram Stavanger
  • 4,158
  • 9
  • 35
  • 63

3 Answers3

31

This should do it:

const arr = ["foo", "bar"];

const obj = {
  foo: 1,
  bar: "hello"
};

const hasAllKeys = arr.every(item => obj.hasOwnProperty(item));

console.log(hasAllKeys);

Array.prototype.every() returns true if the passed function returns true for every item in the array.
Object.prototype.hasOwnProperty() is pretty self-explanatory.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • If `obj` additionally has `baz = 'test'`, this will fail- so it's not inclusive that the keys and the array match perfectly – Matt Fletcher Jan 25 '19 at 11:50
  • @MattFletcher: This code only checks if all the key in the array are present on the object. It does not care about extra keys, as the original question explicitly demonstrated to be a requirement. This was never meant to be a perfect one-on-one match. – Cerbrus Jan 25 '19 at 11:58
  • I know, I just thought I'd highlight it for anyone else looking for exact key matching – Matt Fletcher Jan 25 '19 at 12:08
9

You could iterate the array and check for the key with in operator

The in operator returns true if the specified property is in the specified object.

The difference between in operator and Object#hasOwnProperty is, in checks all properties, even the ones from the prototype, like toString (as in the example) and Object#hasOwnProperty checks only own properties, without the properties from the prototypes.

function checkKeys(keys, object) {
    return keys.every(function (key) {
        return key in object;
    });
}

function checkOwnKeys(keys, object) {
    return keys.every(function (key) {
        return object.hasOwnProperty(key);
    });
}

var arr = ["foo", "bar", "toString"],
    obj = { foo: 1, bar: "hello" };

console.log(checkKeys(arr, obj));                // true
console.log(checkOwnKeys(arr, obj));             // false
console.log(checkOwnKeys(["foo", "bar"], obj));  // true
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • I think it'd be useful to mention the [difference](http://stackoverflow.com/questions/13632999/if-key-in-object-or-ifobject-hasownpropertykey) between `in` and `hasOwnProperty`. Specifically: _"The in operator returns true for properties in the prototype chain."_ Still, valid answer. – Cerbrus Jan 03 '17 at 09:11
0

We can check in the following way

const arr = ["foo", "bar"];

const obj = {
  foo: 1,
  bar: "hello"
};

const hasAllKeys = label => Object.prototype.hasOwnProperty.call(obj, label);

console.log(arr.every(item => hasAllKeys(item)));
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133