Iggsy
Iggsy

Reputation: 113

How to get data from nested JSON in TypeScript?

I have followin problem:

enter image description here

When I tied to get addons I just get [] but web console show me that I have addons.

My json structure:

enter image description here

I just tried to do this:

console.log(vehicle.montage_card.addons); but I have this result: []

What am I doing wrong?

EDIT

I have PrimeNg table, this is little part of this table:

      <ng-template let-col let-vehicle="rowData" let-index="rowIndex" pTemplate="editor">
        <p-multiSelect *ngIf="!viewMode && (continuationContract == 0)" name="client_vehicles{{index}}" [showToggleAll]="false" [style]="{'width':'100%'}"
          [options]="clientVehiclesOptions" defaultLabel="Open list" [ngModel]="vehicle.assignedContractsID" (ngModelChange)="onSelectedClientVehiclesChange($event, vehicle)"
          maxSelectedLabels=0 selectedItemsLabel="Wybrano: {0}." (onChange)="checkSelectedClientVehiclesQuantity(vehicle)"
          [disabled]="viewMode">
        </p-multiSelect>
      </ng-template>

And this is my function when I console.log the vehicle:

onSelectedClientVehiclesChange(event: any, vehicle: Vehicle) {

let temp: any[] = [];
let numberExists: boolean = false;
console.log("vehicle");
console.log(vehicle);
console.log(vehicle.montage_card.addons);

event.forEach(element => {
  temp.push(this.clientVehicles.client_vehicles.find(clientVehicle => clientVehicle.id == element));
});
}

Upvotes: 0

Views: 1892

Answers (2)

Omid Farhang
Omid Farhang

Reputation: 46

Try using following format:

console.log(vehicle['montage_card']['addons']);

Upvotes: -1

wang
wang

Reputation: 1780

When you output the data, addons were not populated but there is a place to populate data which is later than output or using the data in timely manner. For example, populating data from the server using ajax.

Since the data you printed is object which means the reference to the memory space, when you open this after data populated in console, you will see addons.

That's why it says no addons but has addons when you expand it.

To use data, make sure you use it after data population. Pay attention to asynchronous actions.

Upvotes: 2

Related Questions