1

I have object a and object b .I want to check key of b exist in a or not without using loop how can I do this.

var a = {name1: "hello", game1: "no games", name2: "world"}
var b = {name1:'hello world'}

Yes I can do this using loop. First I can get all the keys of b in array and i can take each key at a time and find by using .hasOwnProperty() but I am looking for the method without using loop how is it possible.

5
  • 6
    Object.keys(b)[0] in a, but if b has more than one key then you will need a loop, no way around that. Commented Jan 15, 2018 at 14:26
  • if b is multiple objects then you need to use map Object.keys(b).map(t=>a.hasOwnProperty(t)) Commented Jan 15, 2018 at 14:28
  • 2
    Why the restriction on a loop? All map/reduce/filter are loops too Commented Jan 15, 2018 at 14:33
  • 1
    Please update your question to detail if you want to check more than one key from B - people are confused Commented Jan 15, 2018 at 14:37
  • If you don't want loops, use recursion. But what have you gained? Commented Jan 15, 2018 at 14:38

4 Answers 4

3

Try this:

var a = {name1: "hello", game1: "no games", name2: "world"};
var b =  {name1:'hello world'};

var exists = Object.keys(a).includes(Object.keys(b)[0])
console.log(exists);

In case there are multiple keys in b to ckeck in a, it should be:

var a = {name1: "hello", game1: "no games", name2: "world"};
var b =  {name1:'hello world', game1: "no games"};

var exists = Object.keys(b).every(bKey => Object.keys(a).includes(bKey));
console.log(exists);

Or just:

var a = {name1: "hello", game1: "no games", name2: "world"};
var b =  {name1:'hello world', game1: "no games"};

var exists = Object.keys(b).every(bKey => bKey in a);
console.log(exists);

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

Comments

2

If the keys are not known in advance (and there can be more than one key to check), you can use this trick:

    var a = {name1: "hello", game1: "no games", name2: "world"}
    var b =  {name1:'hello world'}
    
    // Extract keys from a
    var keysA = Object.keys(a);

    // For each key from b, check if a includes it
    // See [1] if needed for every()
    console.log(
      Object.keys(b).every(function(k) { return keysA.includes(k); })
    );

Admittedly, this is very similar to using a loop.

[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every

Comments

1
You can also try underscore.js function _.findKey(object, predicate=_.identity])

Example:-
var users = {
  'barney':  { 'age': 36, 'active': true },
  'fred':    { 'age': 40, 'active': false },
  'pebbles': { 'age': 1,  'active': true }
};

_.findKey(users, function(o) { return o.age < 40; });

Output => 'barney' (iteration order is not guaranteed)

Comments

-1

The simplest way is:

console.log('game1' in {name1: "hello", game1: "no games", name2: "world"})

4 Comments

@AniketSahrawat: You're making the assumption that the key name is known in advance. If that was the case, then what's the significance of it being an object property?
This doesn't answer the question. The OP doesn't want to check a single key, but all keys in b.
Question was: "..I want to check key of b exist in a or not..". Not all keys in b
first I can get all the keys of b in array and i can take each key at a time and find

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.