Find out if objects have same keys












0














I have a pair of objects. I'm trying to find out if they both have the same keys, like so:



let a = { user1: true, user2: true }
let b = { user1: true, user3: true }
hasSameKeys(a, b) => false

let a = { user1: true, user2: true }
let b = { user2: true, user1: true }
hasSameKeys(a, b) => true


I'm also using _underscore.js



Thanks in advance



John S.










share|improve this question




















  • 2




    by "if they have the same keys" do you mean an exact match? One can't have more than the other? Also, please show us what you've tried.
    – zfrisch
    Nov 12 '18 at 18:09
















0














I have a pair of objects. I'm trying to find out if they both have the same keys, like so:



let a = { user1: true, user2: true }
let b = { user1: true, user3: true }
hasSameKeys(a, b) => false

let a = { user1: true, user2: true }
let b = { user2: true, user1: true }
hasSameKeys(a, b) => true


I'm also using _underscore.js



Thanks in advance



John S.










share|improve this question




















  • 2




    by "if they have the same keys" do you mean an exact match? One can't have more than the other? Also, please show us what you've tried.
    – zfrisch
    Nov 12 '18 at 18:09














0












0








0







I have a pair of objects. I'm trying to find out if they both have the same keys, like so:



let a = { user1: true, user2: true }
let b = { user1: true, user3: true }
hasSameKeys(a, b) => false

let a = { user1: true, user2: true }
let b = { user2: true, user1: true }
hasSameKeys(a, b) => true


I'm also using _underscore.js



Thanks in advance



John S.










share|improve this question















I have a pair of objects. I'm trying to find out if they both have the same keys, like so:



let a = { user1: true, user2: true }
let b = { user1: true, user3: true }
hasSameKeys(a, b) => false

let a = { user1: true, user2: true }
let b = { user2: true, user1: true }
hasSameKeys(a, b) => true


I'm also using _underscore.js



Thanks in advance



John S.







javascript algorithm underscore.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 18:13









Frank Modica

6,1082727




6,1082727










asked Nov 12 '18 at 18:07









JohnCdf

96213




96213








  • 2




    by "if they have the same keys" do you mean an exact match? One can't have more than the other? Also, please show us what you've tried.
    – zfrisch
    Nov 12 '18 at 18:09














  • 2




    by "if they have the same keys" do you mean an exact match? One can't have more than the other? Also, please show us what you've tried.
    – zfrisch
    Nov 12 '18 at 18:09








2




2




by "if they have the same keys" do you mean an exact match? One can't have more than the other? Also, please show us what you've tried.
– zfrisch
Nov 12 '18 at 18:09




by "if they have the same keys" do you mean an exact match? One can't have more than the other? Also, please show us what you've tried.
– zfrisch
Nov 12 '18 at 18:09












3 Answers
3






active

oldest

votes


















5














You can test if they have the same number of keys and if every one of one object's keys are contained in the other:






function hasSameKeys(a, b){
return Object.keys(a).length === Object.keys(b).length
&& Object.keys(a).every(k => b.hasOwnProperty(k))
}

let a = { user1: true, user2: true }
let b = { user1: true, user3: true }
console.log(hasSameKeys(a, b) )


a = { user1: true, user2: true }
b = { user2: true, user1: true }
console.log(hasSameKeys(a, b) )








share|improve this answer





























    2














    Try this:






    var a1 = { user1: true, user2: true }
    var b1 = { user2: true, user1: true }
    var c1 = { ...a1, ...b1}
    var a1b1SameKeys = Object.keys(a1).length === Object.keys(b1).length && Object.keys(a1).length === Object.keys(c1).length
    console.log (a1b1SameKeys) // true

    var a2 = { user1: true, user2: true }
    var b2 = { user1: true, user3: true }
    var c2 = { ...a2, ...b2}
    var a2b2SameKeys = Object.keys(a2).length === Object.keys(b2).length && Object.keys(a2).length === Object.keys(c2).length
    console.log (a2b2SameKeys) // false


    var a3 = { user1: true, user2: true, foo: true }
    var b3 = { user1: true, user2: true, bar: false }
    var c3 = { ...a3, ...b3}
    var a2b2SameKeys = Object.keys(a3).length === Object.keys(b3).length && Object.keys(a3).length === Object.keys(c3).length
    console.log (a2b2SameKeys) // false








    share|improve this answer



















    • 1




      Doesn't this just tell you if they have the same number of keys? e.g. if one object had a key foo and the other had a key bar they would both be the same length, but contain different keys.
      – scunliffe
      Nov 12 '18 at 18:16










    • @scunliffe if there were different keys in the two objects, then c1 would be longer. The only way a1, b1 and c1 can all be the same length is if you added no more keys to c1 you merge the tow objects to create it.
      – Mark Meyer
      Nov 12 '18 at 18:18












    • Oh, sorry I missed that c1/c2 were merging the objects. Although this would work, it seems like a bad idea to create a new object (with the potential size of both originals combined) only to check if the keys are the same.
      – scunliffe
      Nov 12 '18 at 18:22










    • yes, if one object had a key foo and the other had a key bar than c1.length will be grather than a1.length or b1.length. I added this example into the answer
      – qiAlex
      Nov 12 '18 at 18:22










    • I agree that Mark's answer is better, just wanted to post some different approach ;)
      – qiAlex
      Nov 12 '18 at 18:24





















    1














    Since you said you're using underscore, you can also use isEqual on sorted arrays of keys:






    function hasSameKeys(a, b) {
    return _.isEqual(_.keys(a).sort(), _.keys(b).sort());
    }

    let a = { user1: true, user2: true };
    let b = { user1: true, user3: true };
    console.log(hasSameKeys(a, b));

    a = { user1: true, user2: true };
    b = { user2: true, user1: true };
    console.log(hasSameKeys(a, b));

    <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>








    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267771%2ffind-out-if-objects-have-same-keys%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      5














      You can test if they have the same number of keys and if every one of one object's keys are contained in the other:






      function hasSameKeys(a, b){
      return Object.keys(a).length === Object.keys(b).length
      && Object.keys(a).every(k => b.hasOwnProperty(k))
      }

      let a = { user1: true, user2: true }
      let b = { user1: true, user3: true }
      console.log(hasSameKeys(a, b) )


      a = { user1: true, user2: true }
      b = { user2: true, user1: true }
      console.log(hasSameKeys(a, b) )








      share|improve this answer


























        5














        You can test if they have the same number of keys and if every one of one object's keys are contained in the other:






        function hasSameKeys(a, b){
        return Object.keys(a).length === Object.keys(b).length
        && Object.keys(a).every(k => b.hasOwnProperty(k))
        }

        let a = { user1: true, user2: true }
        let b = { user1: true, user3: true }
        console.log(hasSameKeys(a, b) )


        a = { user1: true, user2: true }
        b = { user2: true, user1: true }
        console.log(hasSameKeys(a, b) )








        share|improve this answer
























          5












          5








          5






          You can test if they have the same number of keys and if every one of one object's keys are contained in the other:






          function hasSameKeys(a, b){
          return Object.keys(a).length === Object.keys(b).length
          && Object.keys(a).every(k => b.hasOwnProperty(k))
          }

          let a = { user1: true, user2: true }
          let b = { user1: true, user3: true }
          console.log(hasSameKeys(a, b) )


          a = { user1: true, user2: true }
          b = { user2: true, user1: true }
          console.log(hasSameKeys(a, b) )








          share|improve this answer












          You can test if they have the same number of keys and if every one of one object's keys are contained in the other:






          function hasSameKeys(a, b){
          return Object.keys(a).length === Object.keys(b).length
          && Object.keys(a).every(k => b.hasOwnProperty(k))
          }

          let a = { user1: true, user2: true }
          let b = { user1: true, user3: true }
          console.log(hasSameKeys(a, b) )


          a = { user1: true, user2: true }
          b = { user2: true, user1: true }
          console.log(hasSameKeys(a, b) )








          function hasSameKeys(a, b){
          return Object.keys(a).length === Object.keys(b).length
          && Object.keys(a).every(k => b.hasOwnProperty(k))
          }

          let a = { user1: true, user2: true }
          let b = { user1: true, user3: true }
          console.log(hasSameKeys(a, b) )


          a = { user1: true, user2: true }
          b = { user2: true, user1: true }
          console.log(hasSameKeys(a, b) )





          function hasSameKeys(a, b){
          return Object.keys(a).length === Object.keys(b).length
          && Object.keys(a).every(k => b.hasOwnProperty(k))
          }

          let a = { user1: true, user2: true }
          let b = { user1: true, user3: true }
          console.log(hasSameKeys(a, b) )


          a = { user1: true, user2: true }
          b = { user2: true, user1: true }
          console.log(hasSameKeys(a, b) )






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 18:13









          Mark Meyer

          36.7k33059




          36.7k33059

























              2














              Try this:






              var a1 = { user1: true, user2: true }
              var b1 = { user2: true, user1: true }
              var c1 = { ...a1, ...b1}
              var a1b1SameKeys = Object.keys(a1).length === Object.keys(b1).length && Object.keys(a1).length === Object.keys(c1).length
              console.log (a1b1SameKeys) // true

              var a2 = { user1: true, user2: true }
              var b2 = { user1: true, user3: true }
              var c2 = { ...a2, ...b2}
              var a2b2SameKeys = Object.keys(a2).length === Object.keys(b2).length && Object.keys(a2).length === Object.keys(c2).length
              console.log (a2b2SameKeys) // false


              var a3 = { user1: true, user2: true, foo: true }
              var b3 = { user1: true, user2: true, bar: false }
              var c3 = { ...a3, ...b3}
              var a2b2SameKeys = Object.keys(a3).length === Object.keys(b3).length && Object.keys(a3).length === Object.keys(c3).length
              console.log (a2b2SameKeys) // false








              share|improve this answer



















              • 1




                Doesn't this just tell you if they have the same number of keys? e.g. if one object had a key foo and the other had a key bar they would both be the same length, but contain different keys.
                – scunliffe
                Nov 12 '18 at 18:16










              • @scunliffe if there were different keys in the two objects, then c1 would be longer. The only way a1, b1 and c1 can all be the same length is if you added no more keys to c1 you merge the tow objects to create it.
                – Mark Meyer
                Nov 12 '18 at 18:18












              • Oh, sorry I missed that c1/c2 were merging the objects. Although this would work, it seems like a bad idea to create a new object (with the potential size of both originals combined) only to check if the keys are the same.
                – scunliffe
                Nov 12 '18 at 18:22










              • yes, if one object had a key foo and the other had a key bar than c1.length will be grather than a1.length or b1.length. I added this example into the answer
                – qiAlex
                Nov 12 '18 at 18:22










              • I agree that Mark's answer is better, just wanted to post some different approach ;)
                – qiAlex
                Nov 12 '18 at 18:24


















              2














              Try this:






              var a1 = { user1: true, user2: true }
              var b1 = { user2: true, user1: true }
              var c1 = { ...a1, ...b1}
              var a1b1SameKeys = Object.keys(a1).length === Object.keys(b1).length && Object.keys(a1).length === Object.keys(c1).length
              console.log (a1b1SameKeys) // true

              var a2 = { user1: true, user2: true }
              var b2 = { user1: true, user3: true }
              var c2 = { ...a2, ...b2}
              var a2b2SameKeys = Object.keys(a2).length === Object.keys(b2).length && Object.keys(a2).length === Object.keys(c2).length
              console.log (a2b2SameKeys) // false


              var a3 = { user1: true, user2: true, foo: true }
              var b3 = { user1: true, user2: true, bar: false }
              var c3 = { ...a3, ...b3}
              var a2b2SameKeys = Object.keys(a3).length === Object.keys(b3).length && Object.keys(a3).length === Object.keys(c3).length
              console.log (a2b2SameKeys) // false








              share|improve this answer



















              • 1




                Doesn't this just tell you if they have the same number of keys? e.g. if one object had a key foo and the other had a key bar they would both be the same length, but contain different keys.
                – scunliffe
                Nov 12 '18 at 18:16










              • @scunliffe if there were different keys in the two objects, then c1 would be longer. The only way a1, b1 and c1 can all be the same length is if you added no more keys to c1 you merge the tow objects to create it.
                – Mark Meyer
                Nov 12 '18 at 18:18












              • Oh, sorry I missed that c1/c2 were merging the objects. Although this would work, it seems like a bad idea to create a new object (with the potential size of both originals combined) only to check if the keys are the same.
                – scunliffe
                Nov 12 '18 at 18:22










              • yes, if one object had a key foo and the other had a key bar than c1.length will be grather than a1.length or b1.length. I added this example into the answer
                – qiAlex
                Nov 12 '18 at 18:22










              • I agree that Mark's answer is better, just wanted to post some different approach ;)
                – qiAlex
                Nov 12 '18 at 18:24
















              2












              2








              2






              Try this:






              var a1 = { user1: true, user2: true }
              var b1 = { user2: true, user1: true }
              var c1 = { ...a1, ...b1}
              var a1b1SameKeys = Object.keys(a1).length === Object.keys(b1).length && Object.keys(a1).length === Object.keys(c1).length
              console.log (a1b1SameKeys) // true

              var a2 = { user1: true, user2: true }
              var b2 = { user1: true, user3: true }
              var c2 = { ...a2, ...b2}
              var a2b2SameKeys = Object.keys(a2).length === Object.keys(b2).length && Object.keys(a2).length === Object.keys(c2).length
              console.log (a2b2SameKeys) // false


              var a3 = { user1: true, user2: true, foo: true }
              var b3 = { user1: true, user2: true, bar: false }
              var c3 = { ...a3, ...b3}
              var a2b2SameKeys = Object.keys(a3).length === Object.keys(b3).length && Object.keys(a3).length === Object.keys(c3).length
              console.log (a2b2SameKeys) // false








              share|improve this answer














              Try this:






              var a1 = { user1: true, user2: true }
              var b1 = { user2: true, user1: true }
              var c1 = { ...a1, ...b1}
              var a1b1SameKeys = Object.keys(a1).length === Object.keys(b1).length && Object.keys(a1).length === Object.keys(c1).length
              console.log (a1b1SameKeys) // true

              var a2 = { user1: true, user2: true }
              var b2 = { user1: true, user3: true }
              var c2 = { ...a2, ...b2}
              var a2b2SameKeys = Object.keys(a2).length === Object.keys(b2).length && Object.keys(a2).length === Object.keys(c2).length
              console.log (a2b2SameKeys) // false


              var a3 = { user1: true, user2: true, foo: true }
              var b3 = { user1: true, user2: true, bar: false }
              var c3 = { ...a3, ...b3}
              var a2b2SameKeys = Object.keys(a3).length === Object.keys(b3).length && Object.keys(a3).length === Object.keys(c3).length
              console.log (a2b2SameKeys) // false








              var a1 = { user1: true, user2: true }
              var b1 = { user2: true, user1: true }
              var c1 = { ...a1, ...b1}
              var a1b1SameKeys = Object.keys(a1).length === Object.keys(b1).length && Object.keys(a1).length === Object.keys(c1).length
              console.log (a1b1SameKeys) // true

              var a2 = { user1: true, user2: true }
              var b2 = { user1: true, user3: true }
              var c2 = { ...a2, ...b2}
              var a2b2SameKeys = Object.keys(a2).length === Object.keys(b2).length && Object.keys(a2).length === Object.keys(c2).length
              console.log (a2b2SameKeys) // false


              var a3 = { user1: true, user2: true, foo: true }
              var b3 = { user1: true, user2: true, bar: false }
              var c3 = { ...a3, ...b3}
              var a2b2SameKeys = Object.keys(a3).length === Object.keys(b3).length && Object.keys(a3).length === Object.keys(c3).length
              console.log (a2b2SameKeys) // false





              var a1 = { user1: true, user2: true }
              var b1 = { user2: true, user1: true }
              var c1 = { ...a1, ...b1}
              var a1b1SameKeys = Object.keys(a1).length === Object.keys(b1).length && Object.keys(a1).length === Object.keys(c1).length
              console.log (a1b1SameKeys) // true

              var a2 = { user1: true, user2: true }
              var b2 = { user1: true, user3: true }
              var c2 = { ...a2, ...b2}
              var a2b2SameKeys = Object.keys(a2).length === Object.keys(b2).length && Object.keys(a2).length === Object.keys(c2).length
              console.log (a2b2SameKeys) // false


              var a3 = { user1: true, user2: true, foo: true }
              var b3 = { user1: true, user2: true, bar: false }
              var c3 = { ...a3, ...b3}
              var a2b2SameKeys = Object.keys(a3).length === Object.keys(b3).length && Object.keys(a3).length === Object.keys(c3).length
              console.log (a2b2SameKeys) // false






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 12 '18 at 18:21

























              answered Nov 12 '18 at 18:13









              qiAlex

              2,0261724




              2,0261724








              • 1




                Doesn't this just tell you if they have the same number of keys? e.g. if one object had a key foo and the other had a key bar they would both be the same length, but contain different keys.
                – scunliffe
                Nov 12 '18 at 18:16










              • @scunliffe if there were different keys in the two objects, then c1 would be longer. The only way a1, b1 and c1 can all be the same length is if you added no more keys to c1 you merge the tow objects to create it.
                – Mark Meyer
                Nov 12 '18 at 18:18












              • Oh, sorry I missed that c1/c2 were merging the objects. Although this would work, it seems like a bad idea to create a new object (with the potential size of both originals combined) only to check if the keys are the same.
                – scunliffe
                Nov 12 '18 at 18:22










              • yes, if one object had a key foo and the other had a key bar than c1.length will be grather than a1.length or b1.length. I added this example into the answer
                – qiAlex
                Nov 12 '18 at 18:22










              • I agree that Mark's answer is better, just wanted to post some different approach ;)
                – qiAlex
                Nov 12 '18 at 18:24
















              • 1




                Doesn't this just tell you if they have the same number of keys? e.g. if one object had a key foo and the other had a key bar they would both be the same length, but contain different keys.
                – scunliffe
                Nov 12 '18 at 18:16










              • @scunliffe if there were different keys in the two objects, then c1 would be longer. The only way a1, b1 and c1 can all be the same length is if you added no more keys to c1 you merge the tow objects to create it.
                – Mark Meyer
                Nov 12 '18 at 18:18












              • Oh, sorry I missed that c1/c2 were merging the objects. Although this would work, it seems like a bad idea to create a new object (with the potential size of both originals combined) only to check if the keys are the same.
                – scunliffe
                Nov 12 '18 at 18:22










              • yes, if one object had a key foo and the other had a key bar than c1.length will be grather than a1.length or b1.length. I added this example into the answer
                – qiAlex
                Nov 12 '18 at 18:22










              • I agree that Mark's answer is better, just wanted to post some different approach ;)
                – qiAlex
                Nov 12 '18 at 18:24










              1




              1




              Doesn't this just tell you if they have the same number of keys? e.g. if one object had a key foo and the other had a key bar they would both be the same length, but contain different keys.
              – scunliffe
              Nov 12 '18 at 18:16




              Doesn't this just tell you if they have the same number of keys? e.g. if one object had a key foo and the other had a key bar they would both be the same length, but contain different keys.
              – scunliffe
              Nov 12 '18 at 18:16












              @scunliffe if there were different keys in the two objects, then c1 would be longer. The only way a1, b1 and c1 can all be the same length is if you added no more keys to c1 you merge the tow objects to create it.
              – Mark Meyer
              Nov 12 '18 at 18:18






              @scunliffe if there were different keys in the two objects, then c1 would be longer. The only way a1, b1 and c1 can all be the same length is if you added no more keys to c1 you merge the tow objects to create it.
              – Mark Meyer
              Nov 12 '18 at 18:18














              Oh, sorry I missed that c1/c2 were merging the objects. Although this would work, it seems like a bad idea to create a new object (with the potential size of both originals combined) only to check if the keys are the same.
              – scunliffe
              Nov 12 '18 at 18:22




              Oh, sorry I missed that c1/c2 were merging the objects. Although this would work, it seems like a bad idea to create a new object (with the potential size of both originals combined) only to check if the keys are the same.
              – scunliffe
              Nov 12 '18 at 18:22












              yes, if one object had a key foo and the other had a key bar than c1.length will be grather than a1.length or b1.length. I added this example into the answer
              – qiAlex
              Nov 12 '18 at 18:22




              yes, if one object had a key foo and the other had a key bar than c1.length will be grather than a1.length or b1.length. I added this example into the answer
              – qiAlex
              Nov 12 '18 at 18:22












              I agree that Mark's answer is better, just wanted to post some different approach ;)
              – qiAlex
              Nov 12 '18 at 18:24






              I agree that Mark's answer is better, just wanted to post some different approach ;)
              – qiAlex
              Nov 12 '18 at 18:24













              1














              Since you said you're using underscore, you can also use isEqual on sorted arrays of keys:






              function hasSameKeys(a, b) {
              return _.isEqual(_.keys(a).sort(), _.keys(b).sort());
              }

              let a = { user1: true, user2: true };
              let b = { user1: true, user3: true };
              console.log(hasSameKeys(a, b));

              a = { user1: true, user2: true };
              b = { user2: true, user1: true };
              console.log(hasSameKeys(a, b));

              <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>








              share|improve this answer


























                1














                Since you said you're using underscore, you can also use isEqual on sorted arrays of keys:






                function hasSameKeys(a, b) {
                return _.isEqual(_.keys(a).sort(), _.keys(b).sort());
                }

                let a = { user1: true, user2: true };
                let b = { user1: true, user3: true };
                console.log(hasSameKeys(a, b));

                a = { user1: true, user2: true };
                b = { user2: true, user1: true };
                console.log(hasSameKeys(a, b));

                <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>








                share|improve this answer
























                  1












                  1








                  1






                  Since you said you're using underscore, you can also use isEqual on sorted arrays of keys:






                  function hasSameKeys(a, b) {
                  return _.isEqual(_.keys(a).sort(), _.keys(b).sort());
                  }

                  let a = { user1: true, user2: true };
                  let b = { user1: true, user3: true };
                  console.log(hasSameKeys(a, b));

                  a = { user1: true, user2: true };
                  b = { user2: true, user1: true };
                  console.log(hasSameKeys(a, b));

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>








                  share|improve this answer












                  Since you said you're using underscore, you can also use isEqual on sorted arrays of keys:






                  function hasSameKeys(a, b) {
                  return _.isEqual(_.keys(a).sort(), _.keys(b).sort());
                  }

                  let a = { user1: true, user2: true };
                  let b = { user1: true, user3: true };
                  console.log(hasSameKeys(a, b));

                  a = { user1: true, user2: true };
                  b = { user2: true, user1: true };
                  console.log(hasSameKeys(a, b));

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>








                  function hasSameKeys(a, b) {
                  return _.isEqual(_.keys(a).sort(), _.keys(b).sort());
                  }

                  let a = { user1: true, user2: true };
                  let b = { user1: true, user3: true };
                  console.log(hasSameKeys(a, b));

                  a = { user1: true, user2: true };
                  b = { user2: true, user1: true };
                  console.log(hasSameKeys(a, b));

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>





                  function hasSameKeys(a, b) {
                  return _.isEqual(_.keys(a).sort(), _.keys(b).sort());
                  }

                  let a = { user1: true, user2: true };
                  let b = { user1: true, user3: true };
                  console.log(hasSameKeys(a, b));

                  a = { user1: true, user2: true };
                  b = { user2: true, user1: true };
                  console.log(hasSameKeys(a, b));

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 '18 at 18:47









                  slider

                  8,08011129




                  8,08011129






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267771%2ffind-out-if-objects-have-same-keys%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      The Sandy Post

                      Danny Elfman

                      Pages that link to "Head v. Amoskeag Manufacturing Co."