LaVOZs

The World’s Largest Online Community for Developers

'; I would like to return part of object which has object array inside in Javascript - LavOzs.Com

I have an array that contains object arrays as sub-properties. However, I only want to return part of the object. For example,

My original object is:

var  deptObj= 
{
  "requestId": 0,
  "requestId_string": "0",
  "departmentRequests": [
     {
        "persistenceVersion": 3,
        "department": [
             {
               "persistenceId": 111,         
               "persistenceVersion": 0,
               "persistenceVersion_string": "0",
               "userId": 26,
               "userId_string": "26",
               "requestId": 0,
               "requestId_string": "0",
               "deptStatus": "PENDING",
               "groupName": "Group 1 ",
               "groupID": "15",
               "userName": "user",
               "userFirstname": "user1",
               "userLastname": "User",
               "requestDate": "2020-03-26",
               "activityName": "Decision",
               "countPending": 0 
             }
           ],
        "decision": [],
        "comment": "start",
        "approved": [],
        "docIndex": 0
    }
  ]
}

Then, I want to get some part of the object. for example,

{
  "requestId": 0,
  "departmentRequests": [
     {
        "department": [
          {
            "requestId": 0,
            "groupID": "15",
            "requestDate": "2020-03-26",
            "deptStatus": "PENDING",
             "userId": "26",
             "countPending": 0
          }
        ],
        "comment": "start",
        "docIndex": 0
    }
  ]
}

I tried to create JavaScript as follows:

obj = [];

for(var i in deptObj) {  
    for(var j in deptObj[i].department) {  
    var thisObj = {};   
    thisObj.requestDate = deptObj[i].department[j].requestDate; 
    thisObj.userId =  deptObj[i].department[j].userId; 
    thisObj.deptStatus = deptObj[i].department[j].deptStatus ;
    thisObj.activityName = deptObj[i].department[j].activityName;  
    thisObj.countPending = deptObj[i].department[j].countPending; // 
    obj.push(thisObj);            
   }   
}

//return obj;

return  {
  "requestId": deptObj.requestId, 
  "departmentRequests":  obj 
};

I have no idea how to get a sub-array in an object. Any help would be appreciated.. Thanks in advance.

You can use Array.map to iterate over each of the arrays in your objects and extract the desired pieces of data:

var deptObj = {
  "requestId": 0,
  "requestId_string": "0",
  "departmentRequests": [{
    "persistenceVersion": 3,
    "department": [{
      "persistenceId": 111,
      "persistenceVersion": 0,
      "persistenceVersion_string": "0",
      "userId": 26,
      "userId_string": "26",
      "requestId": 0,
      "requestId_string": "0",
      "deptStatus": "PENDING",
      "groupName": "Group 1 ",
      "groupID": "15",
      "userName": "user",
      "userFirstname": "user1",
      "userLastname": "User",
      "requestDate": "2020-03-26",
      "activityName": "Decision",
      "countPending": 0
    }],
    "decision": [],
    "comment": "start",
    "approved": [],
    "docIndex": 0
  }]
}

// get department requests
const reqs = deptObj.departmentRequests.map(function(dr) {
  return {
    "comment": dr.comment,
    "docIndex": dr.docIndex,
    "department": dr.department.map(function(d) {
      return {
        "requestId": d.requestId,
        "groupID": d.groupID,
        "requestDate": d.requestDate,
        "deptStatus": d.deptStatus,
        "userId": d.userId,
        "countPending": d.countPending
      };
    })
  };
});

const res = {
  "requestId": deptObj.requestId,
  "departmentRequests": reqs
};

console.log(res);

Well in short your object can be used as an array it is just that instead of using indexes(0,1,2,3) you use strings ("myfirstelement","mysecondelement","mythirdelement") but you can change that with :

Object.keys()

you just take your object and give it to Object.keys() then you can use all the array functions that exist in javascript for instance slice:

slicing mozilla

this might give yous something like :

let obj = {"something1":"hello","something1":"hi","something2":"greetings","something3":"how are you","something4":"hello","something5":"hello"};

let subarr = Object.keys(obj).slice(2,4);
 
console.log(subarr);

To return parts of an object, you can do object destructuring:

//This basically extracts the values you want
const {requestId, departmentRequests} = deptObj;
// requestId and departmentRequests are now variables containing values from deptObj

Based on what you're trying to do:

const newObj = {};
newObj.requestId = requestId //the variable we extracted
newObj.departmentRequests = departmentRequests

//We should always avoid referencing the original array by making a copy
//However, because of the next step below which uses map(), we don't worry
//because map() returns a new array;

When working with arrays containing objects, to return an array containing only parts of each object, you'll want to use map

Again, based on what you're trying to do:

newObj.departmentRequests.map(department => {
//return an object only containing the parts we want
  return {
    requestId: department.requestId,
    groupID: department.groupID,
    requestDate: department.requestDate,
    deptStatus: department.deptStatus,
    userId: department.userId,
    countPending: department.countPending
  }
})

You should now see newObj containing what you're trying to get.

You need to understand Object structures.

const deptObj = {
  "requestId": 0,
  "requestId_string": "0",
  "departmentRequests": [
    {
      "persistenceVersion": 3,
      "department": [
        {
          "persistenceId": 111,
          "persistenceVersion": 0,
          "persistenceVersion_string": "0",
          "userId": 26,
          "userId_string": "26",
          "requestId": 0,
          "requestId_string": "0",
          "deptStatus": "PENDING",
          "groupName": "Group 1 ",
          "groupID": "15",
          "userName": "user",
          "userFirstname": "user1",
          "userLastname": "User",
          "requestDate": "2020-03-26",
          "activityName": "Decision",
          "countPending": 0
        }
      ],
      "decision": [],
      "comment": "start",
      "approved": [],
      "docIndex": 0
    }
  ]
}
const resObj = {
  requestId: deptObj.requestId,
  departmentRequest: deptObj.departmentRequests.map(a=>{
    return {
      department: a.department.map(d=>{
        return {
          requestId: d.requestId,
          groupId: d.groupID,
          requestDate: d.requestDate,
          deptStatus: d.deptStatus,
          userId: d.userId,
          countPending: d.countPending
        }
      }),
      comment: a.comment,
      docIndex: a.docIndex
    }
  })
};
console.log(resObj);