Home > Back-end >  How to remove outer array of objects
How to remove outer array of objects

Time:02-06

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

CodePudding user response:

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)

  •  Tags:  
  • Related