Search code examples
angular

How do you filter a key inside an object wrapped in an array?


here I have an array like below, then I will send this data to the API as shown below

{
assignStudents: [
    {
       isAssigned: true,
       userID: "xxxxx", 
       fullname: "John 1", 
       acadOrg: "DRM", 
       acadPlan: "Strategy & Growth"
    },
    {
       isAssigned: true,
       userID: "xxxxx",
       fullname: "John 2",
       acadOrg: "DCS", 
       acadPlan: "Computer Science"
    },
    {
       isAssigned: false,
       userID: "xxxxx",
       fullname: "John 3",
       acadOrg: "DRM",
       acadPlan: "Strategy & Growth
    },
 ],
 academicTermID: ""
}

parameters in the API

I have a problem when posting data, so I must first choose the data to be posted, namely userIDand acadOrg.

My question is how to filter the keys inside an object wrapped in an array?


Solution

  • You could use Array.map to keep only the object keys you need before you post the data. Working snippet below.

    var data = {
      assignStudents: [
        {
           isAssigned: true,
           binusID: "1640000616", 
           fullname: "ACHMAD FARID WADJDI", 
           acadOrg: "DRM", 
           acadPlan: "Strategy & Growth"
        },
        {
           isAssigned: true,
           binusID: "0660003053",
           fullname: "AFAN GALIH SALMAN",
           acadOrg: "DCS", 
           acadPlan: "Computer Science"
        },
        {
           isAssigned: false,
           binusID: "BN001047135",
           fullname: "AGUNG YUNANTO",
           acadOrg: "DRM",
           acadPlan: "Strategy & Growth"
        },
      ],
      academicTermID: ""
    };
    
    data.assignStudents = data.assignStudents.map(e => {
        return {binusianID: e.binusID, acadOrg: e.acadOrg};
    });
    
    console.log(data);