I am having an nested array of objects like the below structure. I would like to loop into each object and check whether the particular field matches with a condition or not.If it matches then update that particular object.
Structure
{
"condition": "and",
"rules": [
{
"condition": "and",
"rules": [
{
"field": "deviceName",
"operator": "=",
"value": "device01"
},
{
"field": "temperature",
"operator": ">",
"value": 30
},
{
"field": "mail",
"operator": "to",
"value": "[email protected]"
}
]
},
{
"condition": "and",
"rules": [
{
"field": "deviceName",
"operator": "=",
"value": "device02"
},
{
"field": "voltage",
"operator": "=",
"value": 200
},
{
"field": "log",
"operator": "to",
"value": "[email protected]"
},
{
"condition": "and",
"rules": [
{
"field": "deviceName",
"operator": "=",
"value": "device04"
},
{
"field": "voltage",
"operator": "=",
"value": 200
},
{
"field": "mail",
"operator": "to",
"value": "[email protected]"
}
]
}
]
}
]
}
In the above structure i am checking each rules[] and check whether the field has the value email or log .If it matches then i am setting the type as action else condition.
I have tried map to do it but it works only at the first level. suppose if the object has nested array i couldn't able to filter it.
const queryDetail = this.query.rules.map((query: any) => {
const temp: any = {
condition: {
...query
}
};
if (query.field === 'mail' || query.field === 'log') {
temp.type = 'action';
} else {
temp.type = 'condition';
}
return temp;
});
const updatedQuery = {
condition: this.query.condition,
rules: queryDetail
};
]