0

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

4
  • 3
    this.EmployeeData is an array with two objects. So this.EmployeeData[0] is the first object. There is no error here? Commented Feb 5, 2022 at 13:06
  • 3
    You're accessing the first element of array using [0], it cannot return elements when you're accessing a single index. Either you get the second object using this.EmployeeData[1], or change how array is arranged. Commented Feb 5, 2022 at 13:06
  • 2
    You can not have an object with a duplicated property. Your object would hold name two times, which will simply not work. One of them will be overwritten. Besides, by accessing [0] or [1] of an array, you will get the single element that is stored at said index. It's supposed to return one entry only. Commented Feb 5, 2022 at 13:07
  • 1
    To get the output you want it's just a matter of using this.EmployeeData directly instead of accessing a nested entry by index. If you want the array of objects, then you need to reference the array of objects. Commented Feb 5, 2022 at 13:25

1 Answer 1

1

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)

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

Comments

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.