Skip to main content
16-Pearl
November 15, 2023
Solved

Finding the number of fields in a datashape

  • November 15, 2023
  • 1 reply
  • 1815 views

Hi all

 

In relation to this article: https://community.ptc.com/t5/IoT-Tips/DataShape-Nuances-Tips-Tricks/ta-p/817583?search-action-id=107104588871&search-result-uid=817583,

How can one find the number of fields in a datashape?

I have tried this: 

let result = infotableData.DataShapeName.fields.length;

However this is the error I keep getting:  TypeError: Cannot read property "fields" from undefined.

 

Thank you.

Best answer by Arun_C

Hi @Janicen ,

 

Im not sure why "let result = infotableData.DataShapeName.fields.length;" is not working.

 

But you can try this below code to find the field count from infotable.

 

let dataShapeFields = infotableData.dataShape.fields;
var fieldCount = 0;
for (let fieldName in dataShapeFields) {
fieldCount++;
}
result = fieldCount;

 

Arun_C_0-1700034154141.png

 

Thanks & Regards,

Arun C

1 reply

Arun_C16-PearlAnswer
16-Pearl
November 15, 2023

Hi @Janicen ,

 

Im not sure why "let result = infotableData.DataShapeName.fields.length;" is not working.

 

But you can try this below code to find the field count from infotable.

 

let dataShapeFields = infotableData.dataShape.fields;
var fieldCount = 0;
for (let fieldName in dataShapeFields) {
fieldCount++;
}
result = fieldCount;

 

Arun_C_0-1700034154141.png

 

Thanks & Regards,

Arun C

19-Tanzanite
November 15, 2023

Hi @Janicen  & @Arun_C 

 

You can avoid the loop by below code

 

let dataShapeFields = infoTable.dataShape.fields;
var result = Object.keys(dataShapeFields).length;

 

/VR