2

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.

6
  • I guess you'd have to use a reduce and check for each item if a duplicate exists in the source array, comparing the fields you need to compare. Commented Oct 19, 2022 at 19:02
  • could you perhaps provide an example? Commented Oct 19, 2022 at 19:03
  • Are you trying to detect duplicate data or just remove it? Commented Oct 19, 2022 at 19:05
  • 1
    @Feathercrown just detect. No removal necessary here. There can be tens of entries in this array, and the moment any two entries have the same value for name, description, message and status fields, it'll need to return true, for example. Commented Oct 19, 2022 at 19:07
  • 1
    As @Peterrabbit mentioned, you can use reduce to check for duplicates. Here's an example Commented Oct 19, 2022 at 19:10

3 Answers 3

3

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))

Sign up to request clarification or add additional context in comments.

1 Comment

makes perfect sense, thanks!
1

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));

1 Comment

this is a really nice improvement over the other answer, thank you!
0

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.