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

如何编写jsonlogic.js规则检测组件tableNumber是否存在于参考组件数组?

Answer

Absolutely, this requirement is totally doable! The problem with your current rule comes down to how JSON Logic handles context when iterating over arrays.

Why Your Current Rule Fails

When using the some operator to loop through the refcomponents array, the inner logic runs in the context of each individual element in that array. So when you try to access {"var": "component.tableNumber"}, JSON Logic looks for a component property inside the current array item (which doesn't exist), leading to a null value. That’s why your comparison never returns true.

Fixed JSON Logic Rule

To fix this, you need to explicitly reference the root-level component.tableNumber value instead of relying on the current iteration context. Here’s the corrected rule:

{
  "some": [
    {"var": "refcomponents"},
    {"==": [
      {"var": "tableNumber"},
      {"var": ".component.tableNumber"}
    ]}
  ]
}

The . prefix in ".component.tableNumber" tells JSON Logic to start looking from the root data object, not the current iteration item. This ensures you’re comparing each array element’s tableNumber against the original component.tableNumber value.

Alternative Syntax (Using get)

If your JSON Logic implementation prefers using the get operator for explicit path resolution, you can also write it like this:

{
  "some": [
    {"var": "refcomponents"},
    {"==": [
      {"var": "tableNumber"},
      {"get": ["component.tableNumber", {"var": ""}]}
    ]}
  ]
}

Here, {"var": ""} retrieves the entire root data object, and get extracts the component.tableNumber value from it.

Both versions will correctly check if the component.tableNumber exists in the refcomponents array. Testing with your sample data will return true, as expected.

内容的提问来源于stack exchange,提问作者char m

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:10:14