Return the keys of the properties that are unique in each object

I have this big question if i can do this with simple tools.
If i have a function with 2 objects for parameters and i want to return only the properties of both that are unique, how i could write that?

function uniqueKeys(obj1, obj2) {
}

With a premise like this:

let obj1 = { name: "Jon", lastName: "Snow"};
let obj2 ={name:"Walter", alias:"Heisenberg"}

uniqueKeys(obj1, obj2)  returns  => ["lastName", "alias"];

It would be a lot of help to me! Thanks

Sure, the heart of the algorithm is this challenge: https://www.freecodecamp.org/learn/coding-interview-prep/#algorithms

1 Like

i don’t know why this is not working :__

function clavesUnicas(obj1, obj2) {

  let claves1 = [];

  let claves2 = [];

  let claves = [];

  for (prop in obj1) {

    claves1.push(prop);

    claves.push(prop);

  }

  for (prop in obj2) {

    claves2.push(prop);

    claves.push(prop);

  }

  function objDiff(claves1, claves2) {

    var result = [];

    claves1.forEach(function (item) {

      if (claves2.indexOf(item) < 0 && result.indexOf(item) < 0) {

        result.push(item);

      }

    });

    claves2.forEach(function (item) {

      if (claves1.indexOf(item) < 0 && result.indexOf(item) < 0) {

        result.push(item);

      }

    });

    return result;

  }

  return claves.reduce(objDiff);

}

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

function clavesUnicas(obj1, obj2) {

  var claves1= [];

  var claves2 = [];

  for (prop in obj1) {

    claves1.push(prop);

  }

  for (prop in obj2) {

    claves2.push(prop);

  }

  function objUnicos(claves1, claves2) {

    var result = [];

    claves1.forEach(function (item) {

      if (claves2.indexOf(item) < 0 && result.indexOf(item) < 0) {

        result.push(item);

      }

    });

    claves2.forEach(function (item) {

      if (claves1.indexOf(item) < 0 && result.indexOf(item) < 0) {

        result.push(item);

      }

    });

    return result;

  }

  return result.reduce(objUnicos);

}

if i use this objects …

let obj1 = { name: “Jon”, lastName: “Snow”};
let obj2 ={name:“Walter”, alias:“Heisenberg”}

i should get this:

uniqueKeys(obj1, obj2) returns => [“lastName”, “alias”];

If you want the keys you can use Object.keys. I might also suggest looking at filter and includes (look at the Array methods). Your reduce also isn’t correct and you are running it on something that doesn’t exist, result does not exist in the scope you are using it in.

I might suggest you go through the JS curriculum.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.