0

I need to know the duplicate data regardless of the Id.

This is the array:-

const myArray = [
  {id: "sjdh1", userName: "user 1"  , Message: ["try","test","test"]},
  {id: "sjdh2", userName: "user 2"  , Message: ["test","try","test"]},
  {id: "sjdh3", userName: "user 3"  , Message: ["test"]}, 
  {id: "sjdh4", userName: "user 1"  , Message: ["dummy"]},
  {id: "sjdh5", userName: "user 1"  , Message: ["try","test","test"]},
]

I want to remove the duplicate data. id does not matter

The expected result is:-

const myArray = [
  {id: "sjdh1", userName: "user 1"  , Message: ["try","test","test"]},
  {id: "sjdh2", userName: "user 2"  , Message: ["test","try","test"]},
  {id: "sjdh3", userName: "user 3"  , Message: ["test"]}, 
  {id: "sjdh4", userName: "user 1"  , Message: ["dummy"]}, 
]

Removing the value if the UserName and the Message array is completely Same if the Message array is different then the object should not be considered the duplicate one :

So, I tried to use the set method but I found that it is not working because myArray contains the object.

Then I googled the problem and got this solution but it didnot worked for me :-

 const uniqueValues = new Set(array.map(v => v.name));

It gives me the only one key with it's value

1

3 Answers 3

0

This should suffice for now:

const myArray = [
    {id: "sjdh1", userName: "user 1"  , Message: ["try","test","test"]},
    {id: "sjdh2", userName: "user 2"  , Message: ["test","try","test"]},
    {id: "sjdh3", userName: "user 3"  , Message: ["test"]}, 
    {id: "sjdh4", userName: "user 1"  , Message: ["dummy"]},
    {id: "sjdh5", userName: "user 1"  , Message: ["try","test","test"]},
  ]
function unique(){
    const uniqueKeys = ['userName','Message'];
    const keys = ['id'];
    const uniqueObj = {}
    myArray.forEach((arrObj)=>{
        const strKey = {};
        const strObj = {};
        uniqueKeys.forEach((k)=>{
            strKey[k] = arrObj[k]
        });
        keys.forEach((key)=>{
            strObj[key] = arrObj[key];
        })
        if(!uniqueObj[JSON.stringify(strKey)])
            uniqueObj[JSON.stringify(strKey)] = strObj;
    });
    const output = Object.keys(uniqueObj).map((key)=>{
        return {
            ...uniqueObj[key],
            ...JSON.parse(key)
        }
    })
    console.log(output);
}

unique();

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

7 Comments

can you explain you solution .
can you elaborate a little
yep you got it @Abishek Kumar
@Sarojregmi I basically did a string compare here with JSON.stringify, when I created the key.
and assigned the other value to that key, then iterated over that new object and created an array
|
0

Tried a concise approach

const myArray = [{
    id: "sjdh1",
    userName: "user 1",
    Message: ["try", "test", "test"]
  },
  {
    id: "sjdh2",
    userName: "user 2",
    Message: ["test", "try", "test"]
  },
  {
    id: "sjdh3",
    userName: "user 3",
    Message: ["test"]
  },
  {
    id: "sjdh4",
    userName: "user 1",
    Message: ["dummy"]
  },
  {
    id: "sjdh5",
    userName: "user 1",
    Message: ["try", "test", "test"]
  },
]

function compareObj(obj1, obj2) {
  return obj1.Message.every((msg) => (obj2.Message.includes(msg))) && obj1.userName == obj2.userName;
}

function unique() {
  const uniqueArr = []
  myArray.forEach((arrObj) => {
    if (!uniqueArr.some((uObj) => compareObj(uObj, arrObj)))
      uniqueArr.push(arrObj);
  });
  console.log(uniqueArr);
}

unique();

2 Comments

@Sarojregmi here is another approach
nice Thank you this a little more easy and clear thank you sir
0

We can check the array contents and also the order of the elements to check for the array equality.

Then using Array#reduce we can check which arrays were already seen during the iteration and not consider their index when filtering the original array:

const myArray = [{id: "sjdh1", userName: "user 1"  , Message: ["try","test","test"]}, {id: "sjdh2", userName: "user 2"  , Message: ["test","try","test"]}, {id: "sjdh3", userName: "user 3"  , Message: ["test"]}, {id: "sjdh4", userName: "user 1"  , Message: ["dummy"]}, {id: "sjdh5", userName: "user 1"  , Message: ["try","test","test"]}];

const isArrEqual = (aOne, aTwo) => (aOne && aTwo) && (aOne.length === aTwo.length) && (aOne.every((e, i) => aTwo[i] === e));

const removeDups = (arr) => {
  const res = arr.reduce((r, o, i) => {
    //If the current array and userName is not seen before add it and its index
    if(!r.seen.some(({message, userName}) => isArrEqual(message, o.Message) && userName === o.userName)){
     r.seen.push({message: o.Message, userName: o.userName});
     r.idx.push(i);
    }
    return r;
  //Maintain an object which stores already visited arrays and their indexes
  }, {seen: [] , idx: []} );
  //Filter and return those arrays which occured only once
  return res.idx.map(i => arr[i]);
}
console.log(removeDups(myArray));

3 Comments

I guess OP wanted, the uniqueness on two keys UserName and Message .
@Sarojregmi I updated the answer and checked for both the userName and Message array. Please check if this works for you
@Saroj regmi please accept or upvote if you found it useful, Thanks!

Your Answer

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