How would I access the value of statusCode
for example?
{
res: IncomingMessage {
statusCode: 200,
statusMessage: '',
}
}
How would I access the value of statusCode
for example?
{
res: IncomingMessage {
statusCode: 200,
statusMessage: '',
}
}
It depends on the initial object.
If outer brackets is valid then you can access it like property name:
object.res.statusCode
or you can do it by propertyName:
object['res']['statusCode']
If outer brackets is not valid and you wrote them just for wrapping up. Then you can access status code the same way, just without mentioning res.
Optional Chaining.
Not widely supported in older browsers:
data?.res?.statusCode
Using dot notation:
data.res && data.res.statusCode;
Using property access notation:
data['res'] && data['res']['statusCode']
Using ternary operator:
data['res'] ? data["res"]["statusCode"] : -1
JSONObject.res.statusCode
is a correct approach.
In case that you have something like this (Replace JSONObject with the name of your JSON) :
let JSONObject = {
res: IncomingMessage {
statusCode: 200,
statusMessage: ''
}
}
Your Object structure is wrong maybe you meant this
ob={ res:{ IncomingMessage :{ statusCode: 200,statusMessage: ''}}}
console.log(ob.res.IncomingMessage.statusCode)