简体   繁体   English

如何从TypeScript中的嵌套JSON获取数据?

[英]How to get data from nested JSON in TypeScript?

I have followin problem: 我有Followin问题:

在此处输入图片说明

When I tied to get addons I just get [] but web console show me that I have addons. 当我束缚获取附加组件时,我只获得[],但Web控制台向我显示我具有附加组件。

My json structure: 我的json结构:

在此处输入图片说明

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: 我有PrimeNg表,这是此表的一小部分:

  <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: 这是我在console.log车辆时的功能:

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));
});
}

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. 例如,使用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. 注意异步操作。

尝试使用以下格式:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM