0

I have an object array of an array in object. I would like to remove duplicate objects inside an object array of the object array.

var arr = {

 "departsObj": {

"departments": [
  {
    "department": [
      {
        "groupID": "21",
        "groupName": "group21",
        "requestDate": "2020-01-24",           
      },
      {
        "groupID": "28",
        "groupName": "group28",
        "requestDate": "2020-01-24",            
      }
    ] 
  },
  {
    "department": [
      {
        "groupID": "28",
        "groupName": "group28",
        "requestDate": "2020-01-24",
      },
      {
        "groupID": "20",
        "groupName": "group20",
        "requestDate": "2020-01-24",
      },
      {
        "groupID": "30",
        "groupName": "group30",
        "requestDate": "2020-01-24",
      }
    ] 
  }
]
 }
 }

I need to return the array :

{
  "departsObj": {

"departments": [
  {
    "department": [
      {
        "groupID": "21",
        "groupName": "group21",
        "requestDate": "2020-01-24",           
      },
      {
        "groupID": "28",
        "groupName": "group28",
        "requestDate": "2020-01-24",            
      }
    ] 
  },
  {
    "department": [ 
      {
        "groupID": "20",
        "groupName": "group20",
        "requestDate": "2020-01-24",
      },
      {
        "groupID": "30",
        "groupName": "group30",
        "requestDate": "2020-01-24",
      }
    ] 
  }
]
 }
  }

I have tried :

const arr = departsObj.departments;

var result = arr.reduce((unique, o) => {
if(!unique.some(obj => obj.department === o.department)) {
  unique.push(o);
}
return unique;
},[]);

return result;

But I still return duplicate objects. I am struggling to return the value from an object array of the array objects.

Any help would be appreciated.

Thanks in advance.

2 Answers 2

2

One of the reasons your code is not working is that you can't use a simple array comparison to determine whether or not your arrays contain duplicate values (in addition, you are reducing an object to an array even though your expected output is an object).

You could use a Set to keep track of each groupID that has already been seen in order to dedup the arrays in your nested object.

const obj = { departsObj: { departments: [{ department: [{ groupID: "21", groupName: "group21", requestDate: "2020-01-24" }, { groupID: "28", groupName: "group28", requestDate: "2020-01-24" } ] }, { department: [{ groupID: "28", groupName: "group28", requestDate: "2020-01-24" }, { groupID: "20", groupName: "group20", requestDate: "2020-01-24" }, { groupID: "30", groupName: "group30", requestDate: "2020-01-24" }] }] } };
const seen = new Set();
obj.departsObj.departments = obj.departsObj.departments.filter((d) => {
  d.department = d.department.filter((g) => {
    if (!seen.has(g.groupID)) {
      seen.add(g.groupID);
      return true;
    }
  
    return false;
  });
  
  if (d.department.length) {
    return true;
  }
  
  return false;
});

console.log(obj);
// {"departsObj":{"departments":[{"department":[{"groupID":"21","groupName":"group21","requestDate":"2020-01-24"},{"groupID":"28","groupName":"group28","requestDate":"2020-01-24"}]},{"department":[{"groupID":"20","groupName":"group20","requestDate":"2020-01-24"},{"groupID":"30","groupName":"group30","requestDate":"2020-01-24"}]}]}}

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

Comments

1

You can maintain a dictionary of visited group, while looping through each department groups. so that you can easily identify the duplicate and remove it from array. Here i used slice() to make a copy of the department array, since we are modifying the original array inside loop.

const arr = {
  "departsObj": {
    "departments": [{
        "department": [{
            "groupID": "21",
            "groupName": "group21",
            "requestDate": "2020-01-24",
          },
          {
            "groupID": "28",
            "groupName": "group28",
            "requestDate": "2020-01-24",
          }
        ]
      },
      {
        "department": [{
            "groupID": "28",
            "groupName": "group28",
            "requestDate": "2020-01-24",
          },
          {
            "groupID": "20",
            "groupName": "group20",
            "requestDate": "2020-01-24",
          },
          {
            "groupID": "30",
            "groupName": "group30",
            "requestDate": "2020-01-24",
          }
        ]
      }
    ]
  }
};

const {
  departments
} = arr.departsObj;


// to persist the list of group visited while looping
const visited = {};

departments.forEach(({
  department
}) => {
  // make a copy, since we are modifying the original array inside the loop
  department.slice().forEach((group) => {
    // if group is visited already, remove it
    if (visited[group.groupID]) {
      const index = department.indexOf(group);
      department.splice(index, 1);
    } else {
      // mark the group as visited
      visited[group.groupID] = 1;
    }
  });
});

console.log(arr);

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.