有没有更简单的方法来提取/解析未固定的嵌套对象值? [英] Is there a simpler way to extract/parse nested object values that is not fixed?

查看:58
本文介绍了有没有更简单的方法来提取/解析未固定的嵌套对象值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Zapier的代码从Webhook的结果中解析特定的嵌套对象(Sign_UP_Appointment)

I am trying to parse a specific nested object (Sign_UP_Appointment) from the results of a webhook using code by Zapier

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
        }
    ]
}
}

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

I want to return only the Custom Field (Sign_Up_Appointment__c). I tried the code below but the issue is on subsequent results the order changes. Is there way to filter out only the Sign_Up_Appointment__c object?

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

Zapier Platform团队的

推荐答案

David在这里.

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

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

如果数组中不存在带有该 field_id 的字段,则

SignUpDate 将为 undefined (我不确定这有多大可能)

SignUpDate will be undefined if there's not a field with that field_id in the array (I'm not sure how likely this is)

这篇关于有没有更简单的方法来提取/解析未固定的嵌套对象值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆