我有一个嵌套的对象数组,如下面的结构。我想循环到每个对象,并检查特定字段是否与条件或不,如果它匹配然后更新那个特定的对象。
结构
{
"condition": "and",
"rules": [
{
"condition": "and",
"rules": [
{
"field": "deviceName",
"operator": "=",
"value": "device01"
},
{
"field": "temperature",
"operator": ">",
"value": 30
},
{
"field": "mail",
"operator": "to",
"value": "edison@gmail.com"
}
]
},
{
"condition": "and",
"rules": [
{
"field": "deviceName",
"operator": "=",
"value": "device02"
},
{
"field": "voltage",
"operator": "=",
"value": 200
},
{
"field": "log",
"operator": "to",
"value": "edison@gmail.com"
},
{
"condition": "and",
"rules": [
{
"field": "deviceName",
"operator": "=",
"value": "device04"
},
{
"field": "voltage",
"operator": "=",
"value": 200
},
{
"field": "mail",
"operator": "to",
"value": "edison@gmail.com"
}
]
}
]
}
]
}
我试过用地图来做,但它只在第一层起作用。假设对象有嵌套数组,我无法过滤它。
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
};