简体   繁体   English

使用Javascript从Woocommerce JSON webhook提取嵌套对象

[英]Extract nested objects from Woocommerce JSON webhook using Javascript

I am trying to access the third object that is nested within the "meta_data" array from a Woocommerce webhook to access the following properties: 我正在尝试从Woocommerce Webhook访问嵌套在“ meta_data”数组中的第三个对象,以访问以下属性:

"value": "FEB-ID-75250",
"label": "Landscape JPG"

I can get at the first object in the array that returns "Option One" with 我可以找到返回“ Option One”的数组中的第一个对象

event.line_items[0].meta_data[0].value but I unsure how to get at objects in a similar way you would access arrays with [1] event.line_items[0].meta_data[0].value但我不确定如何以类似的方式获取对象,您将使用[1]访问数组

I can access the email and order number easily with the following 我可以使用以下内容轻松访问电子邮件和订单号

var orderNumber = event.number;

var clientEmail = event.billing.email;

How can I access the third object within an array and get at its properties? 如何访问数组中的第三个对象并获取其属性?

{
    "customer_note": "",
    "billing": {
    "email": "mail@example.com"
    },
    "cart_hash": "f4bf32719d056bac6d6442ed6598cc9d",
    "meta_data": [
    {
        "id": 812,
        "key": "_wcj_order_number",
        "value": "2012371633"
    }
    ],
    "line_items": [
    {
        "id": 7,
        "meta_data": [
        {
            "id": 70,
            "key": "Options",
            "value": "Option One"
        },
        {
            "id": 71,
            "key": "File Upload",
            "value": "mountain.jpg | https://example.com/uploads/130463-mountain.jpg | (£0.00)"
        },
        {
            "id": 72,
            "key": "_WC_order_meta_data",
            "value": [
            {
                "type": "select",
                "name": "select-1555526171583",
                "label": "Options",
                "value": {
                "2": {
                    "i": 2,
                    "value": "FEB-ID-75250",
                    "label": "Landscape JPG"
                }
                },
                "is_fee": false,
                "form_data": {
                "type": "select",
                "form_rules": {
                    "pric_cal_option_once": false,
                    "pric_use_as_fee": false,
                    "fee_label": "Fee",
                    "render_after_acb": false,
                    "disp_hide_options_price": false
                }
                },
                "meta_id": 70
            },
            {
                "label": "File Upload",
                "value": {
                "file_name": "mountain.jpg"
                },
                "is_fee": false,
                "price": 0.0001,
                "quanity_depend": false,
                "form_data": {
                "type": "file",
                "elementId": "wc-file-1549637467462",
                "label": "File Upload",
                "enablePrice": true,
                "pricingType": "multiply",
                "price": "0.0001",
                "ajax_upload": true,
                "name": "file-1549637467462",
                "custom_style": true,
                "cl_rule": "show",
                "col": 6,
                "form_id": 63,
                "form_rules": {
                    "pric_cal_option_once": false,
                    "pric_use_as_fee": false,
                    "fee_label": "Fee",
                    "render_after_acb": false,
                    "disp_hide_options_price": false
                }
                },
                "meta_id": 71
            }
            ]
        }
        ],
        "sku": "",
        "price": 0.0002
    }
    ],
    "tax_lines": [],
    "shipping_lines": [],
    "fee_lines": [],
    "coupon_lines": [],
    "refunds": []
}

This link is the type of thing I am looking for but I'm a bit lost with it 该链接是我正在寻找的类型,但是我对此有点迷惑

https://medium.com/javascript-inside/safely-accessing-deeply-nested-values-in-javascript-99bf72a0855a https://medium.com/javascript-inside/safely-accessing-deeply-nested-values-in-javascript-99bf72a0855a

这是下面的答案,我认为meta_data [0]引用了一个数组(唯一存在的一个数组),但实际上使用了该对象

event.line_items[0].meta_data[1].value[0].value[0].value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM