Home > database >  Delete duplicate ID into list of multiple array
Delete duplicate ID into list of multiple array

Time:10-27

How can i eliminate the duplicate inside the assets?

See the image, i need to eliminate the list with ID duplicate, where is the cross. I try with this map and filter, but the problem i lost the rest of the attribute, as a results i need to have the same structure but with out the duplicate id inside the assets[]

    let known = new Set();
let listArrayDoc = this.documentsfiltered.map(subarray => subarray['assets'].filter(item => !known.has(item.id) && known.add(item.id)));


[
  {
    "id": 198406,
    "description": "4. Monitor, assess, discuss and report on the implementation of all Development Agenda Recommendations",
    "additionalInfo": [],
    "assets": [
      {
        "id": 22116,
        "name": "Completion Report of the Development Agenda (DA) Project on Tools for Successful DA Project Proposals.",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      },
      {
        "id": 22114,
        "name": "Progress Reports – Ongoing Development Agenda Projects",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      },
      {
        "id": 22122,
        "name": "Progress Report on the Implementation of the 45 Development Agenda Recommendations",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      }
    ],
    "refId": null
  },
  {
    "id": 198407,
    "description": "5. Consideration of work program for implementation of adopted recommendations",
    "additionalInfo": [],
    "assets": [
      {
        "id": 22115,
        "name": "C Proposal by the African Group concerning the biennial organization of an International Conference on Intellectual Property and Development.",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      },
      {
        "id": 22118,
        "name": "CDImplementation of the Adopted Recommendations of the Independent Review – Updated Proposal by the Secretariat and Member States Inputs",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      }
    ],
    "refId": null
  },
  {
    "id": 198408,
    "description": "4. Monitor, assess, discuss and report on the implementation of all Development Agenda Recommendations",
    "additionalInfo": [],
    "assets": [
      {
        "id": 22116,
        "name": "Completion Report of the Development Agenda (DA) Project on Tools for Successful DA Project Proposals.",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      },
      {
        "id": 22114,
        "name": " Ongoing Development Agenda Projects",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      },
      {
        "id": 22122,
        "name": "Progress Report on the Implementation of the 45 Development Agenda Recommendations",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      }
    ],
    "refId": null
  }
]

RESULTS NEED TO BE: the last assets empty

[
  {
    "id": 198406,
    "description": "4. Monitor, assess, discuss and report on the implementation of all Development Agenda Recommendations",
    "additionalInfo": [],
    "assets": [
      {
        "id": 22116,
        "name": "Completion Report of the Development Agenda (DA) Project on Tools for Successful DA Project Proposals.",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      },
      {
        "id": 22114,
        "name": "Progress Reports – Ongoing Development Agenda Projects",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      },
      {
        "id": 22122,
        "name": "Progress Report on the Implementation of the 45 Development Agenda Recommendations",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      }
    ],
    "refId": null
  },
  {
    "id": 198407,
    "description": "5. Consideration of work program for implementation of adopted recommendations",
    "additionalInfo": [],
    "assets": [
      {
        "id": 22115,
        "name": "C Proposal by the African Group concerning the biennial organization of an International Conference on Intellectual Property and Development.",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      },
      {
        "id": 22118,
        "name": "CDImplementation of the Adopted Recommendations of the Independent Review – Updated Proposal by the Secretariat and Member States Inputs",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      }
    ],
    "refId": null
  },
  {
    "id": 198408,
    "description": "4. Monitor, assess, discuss and report on the implementation of all Development Agenda Recommendations",
    "additionalInfo": [],
    "assets": [],
    "refId": null
  }
]

CodePudding user response:

In the map you have to return the entire object updating only the assets prop, you can achieve that returning {...subarray, assets: filteredArray} in the map function

let listArrayDoc = [
  {
    "id": 198406,
    "description": "4. Monitor, assess, discuss and report on the implementation of all Development Agenda Recommendations",
    "additionalInfo": [],
    "assets": [
      {
        "id": 22116,
        "name": "Completion Report of the Development Agenda (DA) Project on Tools for Successful DA Project Proposals.",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      },
      {
        "id": 22114,
        "name": "Progress Reports – Ongoing Development Agenda Projects",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      },
      {
        "id": 22122,
        "name": "Progress Report on the Implementation of the 45 Development Agenda Recommendations",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      }
    ],
    "refId": null
  },
  {
    "id": 198407,
    "description": "5. Consideration of work program for implementation of adopted recommendations",
    "additionalInfo": [],
    "assets": [
      {
        "id": 22115,
        "name": "C Proposal by the African Group concerning the biennial organization of an International Conference on Intellectual Property and Development.",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      },
      {
        "id": 22118,
        "name": "CDImplementation of the Adopted Recommendations of the Independent Review – Updated Proposal by the Secretariat and Member States Inputs",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      }
    ],
    "refId": null
  },
  {
    "id": 198408,
    "description": "4. Monitor, assess, discuss and report on the implementation of all Development Agenda Recommendations",
    "additionalInfo": [],
    "assets": [
      {
        "id": 22116,
        "name": "Completion Report of the Development Agenda (DA) Project on Tools for Successful DA Project Proposals.",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      },
      {
        "id": 22114,
        "name": " Ongoing Development Agenda Projects",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      },
      {
        "id": 22122,
        "name": "Progress Report on the Implementation of the 45 Development Agenda Recommendations",
        "isActive": true,
        "sizekb": null,
        "isLocal": false,
        "type": "FILE",
        "url": ""
      }
    ],
    "refId": null
  }
];
let known = new Set();
const res = listArrayDoc.map(subarray => {
    const filteredAssets = subarray['assets'].filter(item => !known.has(item.id) && known.add(item.id) )
    return {...subarray, assets: filteredAssets}
    });
console.log(res)

CodePudding user response:

Solution:

subjectsDocuments: any [] = [];
documentsfiltered: any;
let known = new Set();
            this.documentsfiltered.forEach(element => {
                this.subjectsDocuments.push({id: element['id'], document: element['assets'].filter(item => !known.has(item.id) && known.add(item.id)), description: element['description']});
            });


console.log("Results:", this.subjectsDocuments);

CodePudding user response:

  dataReduce=this.data.reduce((a:any,b:any)=>{
    const filtered={
      ...b, //filtered is an element with all the properties of b
            //and the property "assets" filtered
      assets:b.assets.filter(asset=>{ 
                      //get if exist in any "assets" of a
      let exist=false;
      a.forEach(x=>{
        exist=exist || x.assets.find(as=>as.id==asset.id)!=null
      })
      return !exist  //return !exist
    })}
    a.push(filtered) //add the object filtered to a
    return a
  },[])

stackblitz

  • Related