Currently, I am delving into the world of nested arrays and attempting to locate a specific value within the array based on a condition. The data returned by the API is structured in the following format:
data=
{
"ch_Id": "1234",
"title": "Title",
"status": 4,
"options": [
{
"ch_message": "ABC broad casting ",
"ch_title": "ABC",
"referenceType": "Internal"
}
],
"stage": "active"
}
I am focused on dynamically loading data into a div depending on a certain condition. This means that sometimes the fieldname will be 'ch-Id', and other times it will be 'options[0].ch_message'. How can I programmatically retrieve the value of the fieldname, regardless of what is supplied as input, and bind it to the div?
For 'ch_Id', I understand that I can obtain the value using 'data[ch_Id]', however, when I attempt 'data[options[0].ch_message]', I receive 'undefined'.
displayValue: any;
constructor() { }
ngOnInit(): void {
this.fieldName = resultData[fieldName]; // this could either be ch_Id or options[0].ch_message
// Filter the array value based on fieldName
const filteredData = Object.entries(this.data).filter(([key, value]) => key === this.fieldName)
this.displayValue = filteredData[0][1]
console.log('this.displayValue',this.displayValue);
}