JavaScript How to concat sub object array inside object in javascript? concat object array javascript,concat object array typescript,concat object

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,

Answer:1

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)
Answer:2

Probably dead simple for someone out there, but I want to build a regex that will search across this string: foo[0][2] and return foo 0 2 in the array of matches So far I have come up with is \([...

Probably dead simple for someone out there, but I want to build a regex that will search across this string: foo[0][2] and return foo 0 2 in the array of matches So far I have come up with is \([...

  1. regex capture multiple lines
  2. regex capture multiple groups
  3. regex capture multiple matches
  4. regex capture multiple words
  5. regex capture multiple lines until
  6. regex capture multiple occurrences
  7. regex capture multiple times
  8. regex capture multiple values
  9. regex capture multiple
  10. regex capture multiple patterns
  11. regex capture multiple repeated groups
  12. regex capture multiple digits
  13. regex capture multiple groups python
  14. regex capture multi line
  15. regex multi capture group
  16. regex capture group multiple times
  17. javascript regex capture multiple groups
  18. perl regex capture multiple matches
  19. ruby regex capture multiple
  20. regex capture group multiple matches

I am studying the use of reduce in javascript, and I am trying to restructure an Array of Objects in a generic way - need to be dynamic. flowchart - i get totaly lost I started with this through. ...

I am studying the use of reduce in javascript, and I am trying to restructure an Array of Objects in a generic way - need to be dynamic. flowchart - i get totaly lost I started with this through. ...

I am currently working on a Meteor application that also has a chat functionality. I want to have a list of all conversations that show the most recent message in each conversation. (Similar to ...

I am currently working on a Meteor application that also has a chat functionality. I want to have a list of all conversations that show the most recent message in each conversation. (Similar to ...

Does anyone knows how to directly call a array key of a Map Object. As shown in below code, I can map.get(arr), but not map.get([0, 1, 2, 3]) const map = new Map() const arr = [0,1,2,3] map....

Does anyone knows how to directly call a array key of a Map Object. As shown in below code, I can map.get(arr), but not map.get([0, 1, 2, 3]) const map = new Map() const arr = [0,1,2,3] map....

  1. call array object javascript
  2. call object array php
  3. call array inside object
  4. call an array object
  5. java call array object
  6. call_user_func_array object
  7. call_user_func_array object method
  8. jni call object array method
  9. call_user_func_array object oriented
  10. call_user_func_array object php
  11. array object call function
  12. python call array object