我在对象数组中有一个对象数组。
我想删除对象数组的对象数组内的重复对象。

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",
      }
    ]
  }
]
 }
 }


我需要返回数组:

{
  "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",
      }
    ]
  }
]
 }
  }


我试过了 :

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;


但是我仍然返回重复的对象。
我正在努力从数组对象的对象数组返回值。

任何帮助,将不胜感激。

提前致谢。

最佳答案

您可以在循环访问每个部门组的同时维护访问组的字典。这样您就可以轻松识别重复项并将其从数组中删除。在这里,我使用slice()来复制Department数组,因为我们正在循环内修改原始数组。



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

09-30 19:04
查看更多