How to remove outer array of objects Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to remove outer array of objects without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I wanted to remove outer array of objects. And I have tried to remove the outer array by writing below code.

export class AppComponent implements OnInit {
  name = 'Angular';
  EmployeeData=[
    {"name": [{
        "grade": "A",
        "position": "JSE",
        "data": [{
            "commission": 271,
            "address": "street1"
          } ]}]
    },
    {"name": [{
        "grade": "A",
        "position": "JSE",
        "data": [
          {
            "commission": 271,
            "address": "street1"
          }]}
      ]
    }
  ]
 ngOnInit(){
   
    this.arr = this.EmployeeData[0];
   console.log(this.arr)
  }
}

I am getting below data format as a result. There are two objects inside the array. But I am getting only one object here.

{
  "name": [
    {
      "grade": "A",
      "position": "JSE",
      "data": [
        {
          "commission": 271,
          "address": "street1"
        }
      ]
    }
  ]
}

But my expected output should be

   {"name": [{
       "grade": "A",
       "position": "JSE",
       "data": [{
           "commission": 271,
           "address": "street1"
         } ]}]
   },
   {"name": [{
       "grade": "A",
       "position": "JSE",
       "data": [
         {
           "commission": 271,
           "address": "street1"
         }]}
     ]
   }

Can anyone help me to resolve this

Answer

It is not possible in Javascript for a single thing to be what you want, namely:

  {"name": [{
       "grade": "A",
       "position": "JSE",
       "data": [{
           "commission": 271,
           "address": "street1"
         } ]}]
   },
   {"name": [{
       "grade": "A",
       "position": "JSE",
       "data": [
         {
           "commission": 271,
           "address": "street1"
         }]}
     ]
   }

Each thing enclosed in { and } is a single object

It is possible to have two separate variables, for the two elements, e.g.:

  a = {"name": [{
       "grade": "A",
       "position": "JSE",
       "data": [{
           "commission": 271,
           "address": "street1"
         } ]}]
   }

   b = {"name": [{
       "grade": "A",
       "position": "JSE",
       "data": [
         {
           "commission": 271,
           "address": "street1"
         }]}
     ]
   }

But almost certainly that is not what you wanted.

I think what you wanted was the following:

To remove the outer array and the “name” key

You might try the following:

EmployeeData=[
    {"name": [{
        "grade": "A",
        "position": "JSE",
        "data": [{
            "commission": 271,
            "address": "street1"
          } ]}]
    },
    {"name": [{
        "grade": "A",
        "position": "JSE",
        "data": [
          {
            "commission": 271,
            "address": "street1"
          }]}
      ]
    }
  ]
  
  const arr = EmployeeData.map(thing=>thing.name)
  
  console.log(arr)

But even the above has one extra level of arrays that you probably do not want, so how about this:

EmployeeData=[
    {"name": [{
        "grade": "A",
        "position": "JSE",
        "data": [{
            "commission": 271,
            "address": "street1"
          } ]}]
    },
    {"name": [{
        "grade": "A",
        "position": "JSE",
        "data": [
          {
            "commission": 271,
            "address": "street1"
          }]}
      ]
    }
  ]
  
  const arr = EmployeeData.map(thing=>thing.name[0])
  
  console.log(arr)

And even that doesn’t make much sense, as you probably want to keep “name”

That would be best to do as a separate property, at the same level as “grade”.

For example:

EmployeeData=[
    {"name": [{
        "grade": "A",
        "position": "JSE",
        "data": [{
            "commission": 271,
            "address": "street1"
          } ]}]
    },
    {"name": [{
        "grade": "A",
        "position": "JSE",
        "data": [
          {
            "commission": 271,
            "address": "street1"
          }]}
      ]
    }
  ]
  
  const arr = EmployeeData.map(thing=>thing.name)
  
  console.log(arr)

But even the above has one extra level of arrays that you probably do not want, so how about this:

EmployeeData=[
    {"name": [{
        "name": "John",
        "grade": "A",
        "position": "JSE",
        "data": [{
            "commission": 271,
            "address": "street1"
          } ]}]
    },
    {"name": [{
        "name": "Kelly",
        "grade": "A",
        "position": "JSE",
        "data": [
          {
            "commission": 271,
            "address": "street1"
          }]}
      ]
    }
  ]
  
  const arr = EmployeeData.map(thing=>thing.name[0])
  
  console.log(arr)
We are here to answer your question about How to remove outer array of objects - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji