You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mule 4:仅在布尔字段存在时映射其对应值

按优先级判断布尔字段生成结果值

需求说明

Payload中包含IsTypeA、IsTypeB、IsTypeC三个可选布尔字段(可能仅存在部分),且任何场景下仅有一个字段值为true。需要按IsTypeA → IsTypeB → IsTypeC的优先级,仅在字段存在时使用其值判断结果:若该字段为true则结果为true,否则为false;若三个字段都不存在,结果返回false。

输入示例

完整字段的Payload:

{
 "IsTypeA" : true,
 "IsTypeB" : false,
 "IsTypeC" : false,
 "text1" : "abc",
 "text2" : "def"
}

缺失部分字段的Payload变体:

{
 "IsTypeB" : true,
 "IsTypeC" : false,
 "text1" : "abc",
 "text2" : "def"
}

判断逻辑(中文伪代码)

如果 IsTypeA 存在
  结果:若 IsTypeA 为 true 则返回 true,否则返回 false
否则如果 IsTypeB 存在
  结果:若 IsTypeB 为 true 则返回 true,否则返回 false
否则如果 IsTypeC 存在
  结果:若 IsTypeC 为 true 则返回 true,否则返回 false
否则返回 false

代码实现示例(JavaScript)

function calculateResult(payload) {
    if ('IsTypeA' in payload) {
        return payload.IsTypeA === true;
    } else if ('IsTypeB' in payload) {
        return payload.IsTypeB === true;
    } else if ('IsTypeC' in payload) {
        return payload.IsTypeC === true;
    }
    return false;
}

// 测试示例
const payload1 = {
 "IsTypeA" : true,
 "IsTypeB" : false,
 "IsTypeC" : false,
 "text1" : "abc",
 "text2" : "def"
};
console.log(calculateResult(payload1)); // 输出: true

const payload2 = {
 "IsTypeB" : true,
 "IsTypeC" : false,
 "text1" : "abc",
 "text2" : "def"
};
console.log(calculateResult(payload2)); // 输出: true

const payload3 = {
 "text1" : "abc",
 "text2" : "def"
};
console.log(calculateResult(payload3)); // 输出: false

说明

  • 使用in操作符判断字段是否存在,避免字段值为false时被误判为不存在
  • 严格遵循优先级顺序,只要前面的字段存在就不再检查后续字段
  • 完全匹配需求中“仅在字段存在时使用其值”的规则

内容的提问来源于stack exchange,提问作者Abjt G

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 21:50:26