0

I have an array of objects containing different server configurations. For example, objects with attributes for URL, Username, Password etc... I want to prevent the user from entering duplicate server configuration objects in this array of objects. How can I do that?

I am not looking for reference checks using "===". It only checks for the references. I need to compare all the attributes of each object and push only when the configuration does not exist in the array.

Array: 
[{
    "url": "test.com",
    "username": "Rishabh",
    "apiKeys": "test",
    "jobName": "testJob"
}, {
    "url": "test2.com",
    "username": "Rishabh2",
    "apiKeys": "test",
    "jobName": "testJob"
}]

Input: 
{
    "url": "test2.com",
    "username": "Rishabh2",
    "apiKeys": "test",
    "jobName": "testJob"
}

For the above array and input, the result should be negative (i.e. duplicate entry).

This question, How to determine if object is in array, doesn't answer my question. It clearly says that "car1 and car4 contain the same data but are different instances they should be tested as not equal." I want the opposite. If 2 objects contain the same data, they should be considered equal. Also, I want to compare the property names as well as values.

2 Answers2

0

If you don't want to use some comparison code, you could leverage schema validation packages. Like @hapi/joi (https://github.com/hapijs/joi). The code will look like this

const Joi = require("@hapi/joi");

const input = [{
  "url": "test.com",
  "username": "Rishabh",
  "apiKeys": "test",
  "jobName": "testJob"
}, {
  "url": "test2.com",
  "username": "Rishabh2",
  "apiKeys": "test",
  "jobName": "testJob"
}, {
  "url": "test2.com",
  "username": "Rishabh2",
  "apiKeys": "test",
  "jobName": "testJob"
}];

const schema = Joi.array().items(Joi.object({
  "url": Joi.string().required(),
  "username": Joi.string().required(),
  "apiKeys": Joi.string().required(),
  "jobName": Joi.string().required()
})).unique();

console.log(schema.validate(input));

The key here is unique(); and it works on n number of fields. No manual checks.

The response will tell you about if there was some duplicate or not. In this particular case error will contain

"value" position 2 contains a duplicate value

else the error will be null.

Hope this helps.

Ashish Modi
  • 7,529
  • 2
  • 20
  • 35
  • Thank you for a solution. Although here I am trying not to use any packages, I can use the schema validation package elsewhere. – Rishabh Shukla Feb 12 '20 at 12:16
0

In case the occurrence/order of keys in your object will always be the same. Then you can use the below snippet which stringifies objects and checks if they are same.

var duplicateEntry=false;
existingConfigurationArr.forEach(function(item) {
    if(JSON.stringify(userInputObject) == JSON.stringify(item)){
        duplicateEntry=true;
    }; 
});
alert(duplicateEntry);

In case key order of your JSON object are not sure. follow Sort JavaScript object by key . and utilise this in above code snippet before comparing the object.