Compare key values within object for duplicate

2019-08-04 09:40发布

问题:

I have an object:

myObj = {
  attendent-0-id:"123",
  attendent-0-name:"Bob Smith",
  attendent-1-id:"1234",
  attendent-1-name:"Alex Smith",
  attendent-2-id:"123",
  attendent-2-name:"Charlie Smith",
  attendent-maxGuest:1,
  attendent-party-name:"",
}

I need to create a loop that go through myObj and find all the id's and then compares them for duplicates. So in this case it would log an error because attendent-0-id is equal to attendent-2-id.

If I do find duplicates I need to set a flag to true;

I have tried a bunch of things and am just stuck at this point. Thanks for any help.

回答1:

In your case you can go through myObj using Object.keys() via:

for (const key of Object.keys(obj))

use a plain object as a map to store the previous values of the ids:

const map = {};

use a regex pattern to make sure only the specific ids are evaluated:

const pattern = /^attendent-\d+-id$/;

and then with the help of the map, log the error on duplicate ids:

if (value in map) {
  console.error(`${map[value]} is equal to ${key}, which is ${value}`);        
}

Example:

const myObj = {
  'attendent-0-id': "123",
  'attendent-0-name': "Bob Smith",
  'attendent-1-id': "1234",
  'attendent-1-name': "Alex Smith",
  'attendent-2-id': "123",
  'attendent-2-name': "Charlie Smith",
  'attendent-maxGuest': 1,
  'attendent-party-name': "",
};

function errorOnDuplicateIds(obj) {
  const map = {};
  const pattern = /^attendent-\d+-id$/;
  
  for (const key of Object.keys(obj)) {
    if (pattern.test(key)) {      
      const value = obj[key]
      
      if (value in map) {
        console.error(`${map[value]} is equal to ${key}, which is ${value}`);        
      } else {
        map[value] = key
      }
    }    
  }
}

errorOnDuplicateIds(myObj);



回答2:

const ids = []; // keep track of found ids
Object.keys(myObj).forEach(key => { // iterate over all properties of myObj
    // check if property name is in format "attendent-" *some number* "-id"
    if (/^attendent-\d+-id$/.test(key)) {
        // check if the id has already been found
        if (ids.findIndex(id => id === myObj[key]) !== -1) {
            console.log('error');
        } else {
            ids.push(myObj[key]);
        }
    }
});


回答3:

You can use Object.entries and a Map (keyed by value) for this:

var myObj = {"attendent-0-id":"123","attendent-0-name":"Bob Smith","attendent-1-id":"1234","attendent-1-name":"Alex Smith","attendent-2-id":"123","attendent-2-name":"Charlie Smith","attendent-maxGuest":1, "attendent-party-name":""};

var dupes = [...Object.entries(myObj).reduce(
    (map, [key,val]) => map.set(val, (map.get(val) || []).concat(key)), 
    new Map
).values()].filter(keys => keys.length > 1);

console.log(dupes);

This solution does not give any particular meaning to the format of the keys.

Having said that, your object structure looks suspicious of bad design: you should not have enumerations in your object keys. For that you should use arrays.



回答4:

Object.values(myObj) will create an array of all values and then you can use any way to find duplicate elements in that array.

var myValues = Object.values(myObj); //This will create an array of all values
var uniq = myValues.map((val) => {
  return {count: 1, val: val}
}).reduce((a, b) => {
  a[b.val] = (a[b.val] || 0) + b.count
  return a
}, {});
var duplicates = Object.keys(uniq).filter((a) => uniq[a] > 1)
if (duplicates.length) {
   return true;
} else {
   return false;
}


回答5:

My first advice would be to redefine your object to something more flexible.

let myObject = {
    attendants : [
        { 
            id: "123",
            name: "Bob Smith"
        },
        { 
            id: "456",
            name: "Alex Smith"
        },
        { 
            id: "768",
            name: "Charlie Smith"
        },           
    ],
    maxGuest: 1,
    partyName: ""
};

This will allow you to iterate the attendants.

for (var attendant in myObject.attendants){
     doSomething(attendant.id, attendant.name);
}

You can also sort the attendant:

// Sort by id 
myObject.attendants.sort(function(left, right){
    return left.value - right.value;
});

// Sort by name
myObject.attendants.sort(function (left, right){
    var leftName = left.name.toLowerCase();
    var rightName = right.name.toLowerCase();

    if (leftName < rightName) return -1;
    if (leftName > rightName) return 1;
    return 0;
});

Now, lets assume you don't have a choice. Then it gets complicated.

You need to create (or modify an existent) a sort algorithm so it can use keys that are generated as:

myObject[`attendent-${index}-id`]
myObject[`attendent-${index}-name`]

and keep the pair