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
this.EmployeeDatais an array with two objects. Sothis.EmployeeData[0]is the first object. There is no error here?this.EmployeeData[1], or change how array is arranged.nametwo 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.this.EmployeeDatadirectly instead of accessing a nested entry by index. If you want the array of objects, then you need to reference the array of objects.