12

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
4
  • Where is the magic_function? Commented Jan 3, 2017 at 9:07
  • 4
    @Satpal That's what OP is asking. Commented Jan 3, 2017 at 9:07
  • 6
    Yet another code service? Commented Jan 3, 2017 at 9:08
  • 3
    @PraveenKumar, I was egging OP to atleast share pseudo code . As he has already asked 70+ question rather than just requesting complete code. Totally agree with K. Daniek comment Commented Jan 3, 2017 at 9:14

3 Answers 3

32

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.

Sign up to request clarification or add additional context in comments.

3 Comments

If obj additionally has baz = 'test', this will fail- so it's not inclusive that the keys and the array match perfectly
@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.
I know, I just thought I'd highlight it for anyone else looking for exact key matching
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

1 Comment

I think it'd be useful to mention the difference between in and hasOwnProperty. Specifically: "The in operator returns true for properties in the prototype chain." Still, valid answer.
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)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.