如何在地图中使用过滤器

How to use filter in map

我有以下代码:

%dw 2.0 output application/json var attributeIdMapping =
 ${vault::attributeIdMapping}
--- {
     "objectTypeId": 3,
     "attributes": vars.requestBody mapObject (value, key) ->
        {
            "objectTypeAttributeId": (attributeIdMapping.attributes filter ($.name == key))[0].id,
            "objectAttributeValues": [{
             "value": value,
             "key": key
         }] 
        } }

attributeIdMapping:

{
    "attributes": [
        {
            "name": "accountType",
            "id": "87"
        },
        {
            "name": "accountClass",
            "id": "89"
        },
        {
            "name": "accountName",
            "id": "85"
        },
        {
            "name": "displayName",
            "id": "18"
        },
        {
            "name": "accountCategory",
            "id": "88"
        },
        {
            "name": "accountNumber",
            "id": "84"
        },
        {
            "name": "description",
            "id": "86"
        },
        {
            "name": "accountGroup",
            "id": "90"
        }
    ]
}

vars.requestBody:

{
    "displayName": "TestMulesoft2",
    "description": "Test"
}

但我的过滤器最后显示为空。据我了解,密钥不会传递到地图本身以下的级别。我怎样才能让它工作?

我将所有输入数据都放在脚本中以简化重现。问题似乎是使用运算符 == 来比较字符串和键类型 returns 为空。使用运算符 ~=attempts to coerce 出现在 return 预期结果中:

%dw 2.0 
output application/json 
var attributeIdMapping =
{
    "attributes": [
        {
            "name": "accountType",
            "id": "87"
        },
        {
            "name": "accountClass",
            "id": "89"
        },
        {
            "name": "accountName",
            "id": "85"
        },
        {
            "name": "displayName",
            "id": "18"
        },
        {
            "name": "accountCategory",
            "id": "88"
        },
        {
            "name": "accountNumber",
            "id": "84"
        },
        {
            "name": "description",
            "id": "86"
        },
        {
            "name": "accountGroup",
            "id": "90"
        }
    ]
}
var requestBody = {
    "displayName": "TestMulesoft2",
    "description": "Test"
}
--- {
     "objectTypeId": 3,
     "attributes": requestBody mapObject (value, key) ->
        {
            "objectTypeAttributeId": (attributeIdMapping.attributes filter ($.name ~= key))[0].id,
            "objectAttributeValues": [{
             "value": value,
             "key": key
         }] 
        } }

输出:

{
  "objectTypeId": 3,
  "attributes": {
    "objectTypeAttributeId": "18",
    "objectAttributeValues": [
      {
        "value": "TestMulesoft2",
        "key": "displayName"
      }
    ],
    "objectTypeAttributeId": "86",
    "objectAttributeValues": [
      {
        "value": "Test",
        "key": "description"
      }
    ]
  }
}