extract/parse 不固定的嵌套对象值是否有更简单的方法?

Is there a simpler way to extract/parse nested object values that is not fixed?

我正在尝试使用 Zapier

的代码从 webhook 的结果中解析特定的嵌套对象 (Sign_UP_Appointment)

Webhook 结果

{
"entity":{
    "OPPORTUNITY_ID":24096201,
    "OPPORTUNITY_NAME":"Scott Williams ",
    "OPPORTUNITY_STATE":"OPEN",
    "RESPONSIBLE_USER_ID":1737942,
    "OWNER_USER_ID":1737942,
    "DATE_CREATED_UTC":"2019-04-15T17:02:11.567",
    "DATE_UPDATED_UTC":"2019-04-15T17:02:40.437",
    "VISIBLE_TO":"OWNER",
    "CUSTOMFIELDS":[
        {
            "CUSTOM_FIELD_ID":"Administration_Type__c",
            "FIELD_VALUE":"Summary Administration"
        },
        {
            "CUSTOM_FIELD_ID":"Initial_Appointment__c",
            "FIELD_VALUE":"2019-04-11T20:45:00"
        },
        {
            "CUSTOM_FIELD_ID":"Sign_Up_Appointment__c",
            "FIELD_VALUE":"2019-04-18T21:00:00"
        }
    ],
    "TAGS":[],
    "LINKS":[
        {
            "LINK_ID":205236388,
            "CONTACT_ID":287320999,
            "OPPORTUNITY_ID":24096201
        }
    ]
}
}

我只想 return 自定义字段 (Sign_Up_Appointment__c)。我尝试了下面的代码,但问题在于顺序更改的后续结果。有没有办法只过滤掉 Sign_Up_Appointment__c 对象?

const result = JSON.parse(inputData.body);
return {result: result, SignUpDate: result.entity.CUSTOMFIELDS[3]};

来自 Zapier 平台团队的大卫。

是的!您可以使用 Array.find:

const result = JSON.parse(inputData.body);
return {
  result,
  SignUpDate: result.entity.CUSTOMFIELDS.find(
    f => f.CUSTOM_FIELD_ID === "Sign_Up_Appointment__c"
  )
};

SignUpDate 将是 undefined 如果数组中没有包含 field_id 的字段(我不确定这有多大可能)