In this esp32 tutorial, we will check how to obtain the property names of a JavaScript object created on Espruino, running on the ESP32.
If you haven’t yet checked how to create objects in Espruino, please check this previous tutorial, where it is covered in greater detail.
JavaScript is a very dynamic and flexible language, so there are plenty of possibilities when working with objects, which includes adding new properties to an object after it was created. So, it is useful to have a way of checking the properties of the object at a given time.
In this tutorial we will create an object and then check two different ways of obtaining its property names.
Although we will be running the code on a microcontroller using Espruino, the concepts explained here can be used in regular JavaScript programming.
The tests were performed using a DFRobot’s ESP32 module integrated in a ESP32 development board.
We will start our code by creating an object. We will call it sensorMeasurement and use a data structure that could be used in an object representing a measurement of a sensor, just for illustration purposes.
Our object will have three properties containing numbers and strings, and a fourth property containing a nested object.
var sensorMeasurement = { deviceId: "1234", value: 1, type: "temperature", someNestedProperty : { prop1: 1, prop2: 2 } }
To obtain an array with the names of the properties of our object, we simply need to call the keys method of the Object constructor, passing as input our sensorMeasurement object. We can directly print the output.
console.log(Object.keys(sensorMeasurement));
As an alternative, we can simply use a for..in loop to iterate over all enumerable properties of the object. Then, in each iteration of the loop, we simply print the current property name.
for (var prop in sensorMeasurement){ console.log(prop); }
The final code can be seen below.
var sensorMeasurement = { deviceId: "1234", value: 1, type: "temperature", someNestedProperty : { prop1: 1, prop2: 2 } }; console.log(Object.keys(sensorMeasurement)); for (var prop in sensorMeasurement){ console.log(prop); }
To test the code, we need to send the previous JavaScript commands to the ESP32. If you are not sure how to do it, please check this previous post.
Upon running the commands, you should get an output similar to figure 1.
Figure 1 – Output of the script.
As can be seen in figure 1, in the first highlighted area, we have obtained the names of all the properties of the sensorMeasurement object as an array. Note however that the names of properties of nested objects are not returned, which is why we get “someNestedProperty” in the list, but not “prop1” or “prop2“.
This is the expected behavior and if we have the need to get the names of properties of nested objects we need to implement our custom function.
If we analyze the second highlighted area, we can conclude that the result is similar, but now our code is printing the property names one by one. The names obtained in the second approach match the the ones obtained in the first, as expected.