Home > database >  ES6 - How to check if Multiple properties in an array of objects have duplicate values?
ES6 - How to check if Multiple properties in an array of objects have duplicate values?

Time:10-20

Following is a sample array that I have. The goal is to determine whether there are duplicate entries based on a number of fields (details below) -

values = [
    {
        "name": "123",
        "loginTime": "01:00:00",
        "logoutTime": "01:00:00",
        "description": "test",
        "message": "test2",
        "status": "test3"
    },
    {
        "name": "123",
        "loginTime": "01:00:00",
        "logoutTime": "00:00:00",
        "description": "test",
        "message": "test2",
        "status": "test3"
    },
    {
        "name": "222",
        "loginTime": "01:00:00",
        "logoutTime": "00:00:00",
        "description": "test2",
        "message": "test2",
        "status": "test1"
    }
]

I am aware of the similar looking popular question here, however, the scenario I'm talking about is different. In that question, the user is concerned mainly about the name field only and so the Set based solution works fine. In my case, however, I need to determine the existence of duplicate data if the name, description, message, status fields are the same between any two entries in values (and so not just one field). So from the example list above, the first two entries are duplicates, since all the mentioned fields have the same value between them. At least one of the aforementioned fields will need to have a non-duplicate value here. What would be an ES6 compliant way to detect the duplicate data here?

A Set based solution makes sense for when there's just one field that we'll have to look into to determine the duplication. Not sure how one would do it for multiple fields.

CodePudding user response:

You can combine the key fields into a single value and then use that in a Set. For creating the combined value you may use JSON.stringify on an array of fields:

const hasDuplicates = values =>
    new Set(
        values.map(({name, description, message, status}) =>
            JSON.stringify([name, description, message, status]))
    ).size < values.length;

const values = [{"name": "123","loginTime": "01:00:00","logoutTime": "01:00:00","description": "test","message": "test2","status": "test3"},{"name": "123","loginTime": "01:00:00","logoutTime": "00:00:00","description": "test","message": "test2","status": "test3"},{"name": "222","loginTime": "01:00:00","logoutTime": "00:00:00","description": "test2","message": "test2","status": "test1"}];

console.log(hasDuplicates(values))

CodePudding user response:

function test(values) {
    const tempArr = values.map(item => { 
    return { 
        name: item.name, 
        description: item.description, 
        message: item.message, 
        status: item.status 
    } 
});
   let ignoreIndex = [];
   const indexToSplice = tempArr.map(JSON.stringify).map((item, index) => tempArr.findIndex((obj, i) => {
   if(JSON.stringify(obj) === item && i !== index && !ignoreIndex.includes(index)) {
        ignoreIndex.push(i);
        return true;
    }
    return false;
})).filter(number => number >= 0 ? true : false);
indexToSplice.forEach(index => values.splice(index, 1));
return values;
}

const values = [
    {
        "name": "123",
        "loginTime": "01:00:00",
        "logoutTime": "01:00:00",
        "description": "test",
        "message": "test2",
        "status": "test3"
    },
    {
        "name": "123",
        "loginTime": "01:00:00",
        "logoutTime": "00:00:00",
        "description": "test",
        "message": "test2",
        "status": "test3"
    },
    {
        "name": "222",
        "loginTime": "01:00:00",
        "logoutTime": "00:00:00",
        "description": "test2",
        "message": "test2",
        "status": "test1"
    }
]

console.log(test(values))

CodePudding user response:

A slight improvement over the other answer, to avoid repeating property names:

const keys = ['name', 'description', 'message', 'status'];
const hasDuplicates = values => 
    new Set(values.map(v => JSON.stringify(keys.map(key => v[key]))))
    .size < values.length;

const values = [{"name": "123","loginTime": "01:00:00","logoutTime": "01:00:00","description": "test","message": "test2","status": "test3"},{"name": "123","loginTime": "01:00:00","logoutTime": "00:00:00","description": "test","message": "test2","status": "test3"},{"name": "222","loginTime": "01:00:00","logoutTime": "00:00:00","description": "test2","message": "test2","status": "test1"}];
console.log(hasDuplicates(values));

  • Related