0

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));
});
}
Iggsy
  • 113
  • 1
  • 2
  • 13
  • 5
    And where are you console logging this? Impossible to help if we don't know this. Please show your code. – AT82 Oct 09 '18 at 08:55
  • 1
    Also another note, never post just images of code. Always add code to question as code blocks :) – AT82 Oct 09 '18 at 08:59
  • the way you access the addons is correct like vehicle is the object inside that object there is another object called montage_card , inside that the array of addons consist. Can you post the code – Learner Oct 09 '18 at 09:03
  • is it a ajax call ? where you get the response – Learner Oct 09 '18 at 09:05

2 Answers2

2

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.

wang
  • 1,660
  • 9
  • 20
-1

Try using following format:

console.log(vehicle['montage_card']['addons']);
Omid Farhang
  • 46
  • 1
  • 6
  • using dot and inside brackets you can access the object properties , you can read this one where you use dot vs brackets https://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets – Learner Oct 09 '18 at 09:09