I'm currently learning JS and I need a way to return an object that is similar to the given object but removing key/value pairs that have the same value (i.e. duplicates).
So if I had a given object { a: 1, b: 2, c: 3, d: 1 }:
it should return:
{b: 2, c: 3 }
Similarly, if all key-value pairs in an object had different values, the returned object would simply be the same as the given object.
I think I am close to solving it but I can't figure out what's wrong with my code. Any help would be much appreciated!
const noDuplicateValues = (obj) => {
let result = {};
let keys = Object.keys(obj);
let duplicate;
for(let i = 0; i < keys.length; i++) {
for(let j = i +1; j < keys.length; j++) {
duplicate = false;
if(obj[keys[i]] === obj[keys[j]]) {
duplicate = true;
}
}
if(!duplicate) {
result[keys[i]] = obj[keys[i]];
}
}
return result;
}