user14093271
user14093271

Reputation:

How To find If this Array has duplicate object which Has another array inside it

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

Upvotes: 0

Views: 86

Answers (3)

Amardeep Bhowmick
Amardeep Bhowmick

Reputation: 16908

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

Upvotes: 0

Abishek Kumar
Abishek Kumar

Reputation: 539

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

Upvotes: 0

Abishek Kumar
Abishek Kumar

Reputation: 539

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

Upvotes: 0

Related Questions