I am getting a following object
{
IuW1zvaSABwH4q: {
label: 'Random Image of TypeScript not relavent to coworking',
thumbId: 'd501-f-b601-c8b1-4bd995e',
schemaType: 'xman-assets-image-set'
}
}
Now, I want to access the value of thumbID inside it i.e. d501-f-b601-c8b1-4bd995e
But my root key seems to be dynamic/random (IuW1zvaSABwH4q), How can I access the value inside it?
You can get the values from object and than access the desired key.
let obj = {
IuW1zvaSABwH4q:
{
label: 'Random Image of TypeScript not relavent to coworking',
thumbId: 'd501-f-b601-c8b1-4bd995e',
schemaType: 'xman-assets-image-set'
}
}
let op = Object.values(obj)[0].thumbId
console.log(op)
You can use Array.map
to transform it and Array.forEach
to get it and print it in the console.
const obj = {
IuW1zvaSABwH4q: {
label: 'Random Image of TypeScript not relavent to coworking',
thumbId: 'd501-f-b601-c8b1-4bd995e',
schemaType: 'xman-assets-image-set'
},
YuW1zvaSABwH7q: {
label: 'Random Image of TypeScript not relavent to coworking',
thumbId: 'as90-f-b601-c8b1-4bd9958',
schemaType: 'xman-assets-image-set'
}
};
//for one object
console.log(Object.values(obj)[0].thumbId);
//multiple unknown keys
Object.values(obj).map(ele => ele.thumbId).forEach(th=> console.log(th));
Assuming there is only one property you could access it via the first property.
const obj = { IuW1zvaSABwH4q:
{ label: 'Random Image of TypeScript not relavent to coworking', thumbId: 'd501-f-b601-c8b1-4bd995e', schemaType: 'xman-assets-image-set'
}
};
console.log(obj[Object.getOwnPropertyNames(obj)[0]].thumbId);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With