1

I would like to concat sub object array inside object in javascript. I have an object array inside an object: I would like to return the department sub array into one object array.

var array =
    {
      "departsObjInput": { 
        "departmentRequests": [
          {
            "department": [
              [
                {
                  "groupID": "20",
                  "groupName": "Group20",
                  "requestDate": "2020-01-21",
                  "deptStatus": "PENDING"  
                },
                {
                  "groupID": "21",
                  "groupName": "Group21",
                  "requestDate": "2020-01-21",
                  "deptStatus": "PENDING"  
                },
                {
                  "groupID": "24",              
                  "groupName": "Group24",
                  "requestDate": "2020-01-21",
                  "deptStatus": "PENDING" 
                }
              ],
              [
                {
                  "groupID": "18",
                  "groupName": "Group18",
                  "requestDate": "2020-01-21",
                  "deptStatus": "PENDING"  
                },
                {
                  "groupID": "26",
                  "groupName": "Group26",
                  "requestDate": "2020-01-21",
                  "deptStatus": "PENDING"
                }
              ]
            ] 
          },
          {
            "department": [
              [
                {
                  "groupID": "90",
                  "groupName": "Group90",
                  "requestDate": "2020-01-21",
                  "deptStatus": "PENDING",
                },
                {
                  "groupID": "38",
                  "groupName": "Group38",
                  "requestDate": "2020-01-21",
                  "deptStatus": "PENDING",
                }
              ],
              [
                {
                  "groupID": "37",
                  "groupName": "Group37",
                  "requestDate": "2020-01-21",
                  "deptStatus": "PENDING",
                }
              ]
            ],
          }
        ]
      }
    }

I would like to concat sub array of department like :

  {
  "departsObjInput": { 
    "departmentRequests": [
      {
        "department":  
          [
            {
              "groupID": "20",
              "groupName": "Group20",
              "requestDate": "2020-01-21",
              "deptStatus": "PENDING"  
            },
            {
              "groupID": "21",
              "groupName": "Group21",
              "requestDate": "2020-01-21",
              "deptStatus": "PENDING"  
            },
            {
              "groupID": "24",              
              "groupName": "Group24",
              "requestDate": "2020-01-21",
              "deptStatus": "PENDING" 
            },          
            {
              "groupID": "18",
              "groupName": "Group18",
              "requestDate": "2020-01-21",
              "deptStatus": "PENDING"  
            },
            {
              "groupID": "26",
              "groupName": "Group26",
              "requestDate": "2020-01-21",
              "deptStatus": "PENDING"
            }
          ]

      },
      {
        "department":  
          [
            {
              "groupID": "90",
              "groupName": "Group90",
              "requestDate": "2020-01-21",
              "deptStatus": "PENDING",
            },
            {
              "groupID": "38",
              "groupName": "Group38",
              "requestDate": "2020-01-21",
              "deptStatus": "PENDING",
            },           
            {
              "groupID": "37",
              "groupName": "Group37",
              "requestDate": "2020-01-21",
              "deptStatus": "PENDING",
            }
          ]
         ,
      }
    ]
  }
}

what I tried to :

var deptItem = array.departsObjInput.departmentRequests
var merged = [].concat.apply([], deptItem);
return merged;

I tried other scritps but I could not get the answer. Please help me to find the solution.

Thanks in advance,

2 Answers 2

2

You just need to use forEach loop to access each department array then make it single 1D array using flat()

array.departsObjInput.departmentRequests.forEach(x => {
  x.department = x.department.flat();
})

var array =
    {
      "departsObjInput": { 
        "departmentRequests": [
          {
            "department": [
              [
                {
                  "groupID": "20",
                  "groupName": "Group20",
                  "requestDate": "2020-01-21",
                  "deptStatus": "PENDING"  
                },
                {
                  "groupID": "21",
                  "groupName": "Group21",
                  "requestDate": "2020-01-21",
                  "deptStatus": "PENDING"  
                },
                {
                  "groupID": "24",              
                  "groupName": "Group24",
                  "requestDate": "2020-01-21",
                  "deptStatus": "PENDING" 
                }
              ],
              [
                {
                  "groupID": "18",
                  "groupName": "Group18",
                  "requestDate": "2020-01-21",
                  "deptStatus": "PENDING"  
                },
                {
                  "groupID": "26",
                  "groupName": "Group26",
                  "requestDate": "2020-01-21",
                  "deptStatus": "PENDING"
                }
              ]
            ] 
          },
          {
            "department": [
              [
                {
                  "groupID": "90",
                  "groupName": "Group90",
                  "requestDate": "2020-01-21",
                  "deptStatus": "PENDING",
                },
                {
                  "groupID": "38",
                  "groupName": "Group38",
                  "requestDate": "2020-01-21",
                  "deptStatus": "PENDING",
                }
              ],
              [
                {
                  "groupID": "37",
                  "groupName": "Group37",
                  "requestDate": "2020-01-21",
                  "deptStatus": "PENDING",
                }
              ]
            ],
          }
        ]
      }
    }
    
array.departsObjInput.departmentRequests.forEach(x => {
  x.department = x.department.flat();
})

console.log(array)

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

Comments

2

The array is in array.departsObjInput.departmentRequests.department.

And departmentRequests is an array, you need to index it.

array.departsObjInput.departmentRequests[i].department = [].concat(...array.departsObjInput.departmentRequests[i].department);

1 Comment

Sir array.departsObjInput.departmentRequests is an array you need to loop through it.

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.