动态遍历JSON

时间:2018-08-16 04:35:05

标签: javascript json node.js for-loop ejs

我正在尝试在JSON数据中输出所有客户ID。我正在使用ejs作为模板引擎。到目前为止,我只能通过以下方式成功输出1个客户ID:

<p><%= jsonOrderData.orders[0].customer.id %></p>

当我尝试遍历每个订单时,我得到无法读取未定义的属性'id'

用于循环遍历

    <% for (var i = 0; i < jsonOrderData.orders.length; i++) {%>
        <p>Customer: <%= jsonOrderData.orders[i].customer.id %></p>
    <% }; %>

如果我在客户之后删除.id,错误将消失。然后输出 客户:[对象对象] 50次,即json数据的有效期。

我不明白为什么在没有手动设置索引的循环的情况下正常添加.id时,为什么在循环中无法运行?

JSON数据(削减2个订单)

{
  "orders": [
    {
      "id": 533078016054,
      "email": "email@gmail.com",
      "closed_at": null,
      "created_at": "2018-08-10T05:03:36+01:00",
      "updated_at": "2018-08-10T05:03:37+01:00",
      "number": 52,
      "note": "",
      "token": "f4877048c08eb98180ee5fda34f978bc",
      "gateway": "manual",
      "test": false,
      "total_price": "13.98",
      "subtotal_price": "13.98",
      "total_weight": 0,
      "total_tax": "0.00",
      "taxes_included": false,
      "currency": "GBP",
      "financial_status": "pending",
      "confirmed": true,
      "total_discounts": "0.00",
      "total_line_items_price": "13.98",
      "cart_token": null,
      "buyer_accepts_marketing": false,
      "name": "#MW1052",
      "referring_site": null,
      "landing_site": null,
      "cancelled_at": null,
      "cancel_reason": null,
      "total_price_usd": "18.00",
      "checkout_token": null,
      "reference": null,
      "user_id": 1706983449,
      "location_id": null,
      "source_identifier": null,
      "source_url": null,
      "processed_at": "2018-08-10T05:03:36+01:00",
      "device_id": null,
      "phone": null,
      "customer_locale": null,
      "app_id": 1354745,
      "browser_ip": null,
      "landing_site_ref": null,
      "order_number": 1052,
      "discount_applications": [],
      "discount_codes": [],
      "note_attributes": [],
      "payment_gateway_names": [
        "manual"
      ],
      "processing_method": "manual",
      "checkout_id": null,
      "source_name": "shopify_draft_order",
      "fulfillment_status": null,
      "tax_lines": [],
      "tags": "",
      "contact_email": "email@gmail.com",
      "order_status_url": "https://checkout.shopify.com/1245839385/orders/f4877048c08eb98180ee5fda34f978bc/authenticate?key=redacted",
      "admin_graphql_api_id": "gid://shopify/Order/redacted",
      "line_items": [
        {
          "id": 1350736445494,
          "variant_id": 8725905539126,
          "title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
          "quantity": 1,
          "price": "6.99",
          "sku": "ss15",
          "variant_title": "S / Bottle Green",
          "vendor": "Fruit Of The Loom",
          "fulfillment_service": "manual",
          "product_id": 719146287158,
          "requires_shipping": true,
          "taxable": false,
          "gift_card": false,
          "name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Bottle Green",
          "variant_inventory_management": "shopify",
          "properties": [],
          "product_exists": true,
          "fulfillable_quantity": 1,
          "grams": 0,
          "total_discount": "0.00",
          "fulfillment_status": null,
          "discount_allocations": [],
          "admin_graphql_api_id": "gid://shopify/LineItem/1350736445494",
          "tax_lines": [
            {
              "title": "VAT",
              "price": "0.00",
              "rate": 0.2
            }
          ]
        },
        {
          "id": 1350736478262,
          "variant_id": 8725905440822,
          "title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
          "quantity": 1,
          "price": "6.99",
          "sku": "ss12",
          "variant_title": "S / Heather Grey",
          "vendor": "Fruit Of The Loom",
          "fulfillment_service": "manual",
          "product_id": 719146287158,
          "requires_shipping": true,
          "taxable": false,
          "gift_card": false,
          "name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Heather Grey",
          "variant_inventory_management": "shopify",
          "properties": [],
          "product_exists": true,
          "fulfillable_quantity": 1,
          "grams": 0,
          "total_discount": "0.00",
          "fulfillment_status": null,
          "discount_allocations": [],
          "admin_graphql_api_id": "gid://shopify/LineItem/1350736478262",
          "tax_lines": [
            {
              "title": "VAT",
              "price": "0.00",
              "rate": 0.2
            }
          ]
        }
      ],
      "shipping_lines": [],
      "billing_address": {
        "first_name": "John",
        "address1": "17A Oaklands Business Centre",
        "phone": null,
        "city": "Worthing",
        "zip": "BN11 5LH",
        "province": null,
        "country": "United Kingdom",
        "last_name": "Doe",
        "address2": "Elm Grove",
        "company": null,
        "latitude": 50.8162744,
        "longitude": -0.4010653,
        "name": "Jogn Doe",
        "country_code": "GB",
        "province_code": null
      },
      "shipping_address": {
        "first_name": "John",
        "address1": "17A Oaklands Business Centre",
        "phone": null,
        "city": "Worthing",
        "zip": "BN11 5LH",
        "province": null,
        "country": "United Kingdom",
        "last_name": "Doe",
        "address2": "Elm Grove",
        "company": null,
        "latitude": 50.8162744,
        "longitude": -0.4010653,
        "name": "John Doe",
        "country_code": "GB",
        "province_code": null
      },
      "fulfillments": [],
      "refunds": [],
      "customer": {
        "id": 556974014518,
        "email": "email@gmail.com",
        "accepts_marketing": false,
        "created_at": "2018-06-26T00:26:55+01:00",
        "updated_at": "2018-08-10T05:03:36+01:00",
        "first_name": "John",
        "last_name": "Doe",
        "orders_count": 22,
        "state": "enabled",
        "total_spent": "0.00",
        "last_order_id": 533078016054,
        "note": null,
        "verified_email": true,
        "multipass_identifier": null,
        "tax_exempt": false,
        "phone": null,
        "tags": "",
        "last_order_name": "#MW1052",
        "admin_graphql_api_id": "gid://shopify/Customer/556974014518",
        "default_address": {
          "id": 601657278518,
          "customer_id": 556974014518,
          "first_name": "John",
          "last_name": "Doe",
          "company": null,
          "address1": "17A Oaklands Business Centre",
          "address2": "Elm Grove",
          "city": "Worthing",
          "province": null,
          "country": "United Kingdom",
          "zip": "BN11 5LH",
          "phone": null,
          "name": "John Doe",
          "province_code": null,
          "country_code": "GB",
          "country_name": "United Kingdom",
          "default": true
        }
      }
    },
    {
      "id": 532977778742,
      "email": "james@bungeedesign.com",
      "closed_at": null,
      "created_at": "2018-08-09T22:18:53+01:00",
      "updated_at": "2018-08-09T22:18:53+01:00",
      "number": 51,
      "note": "",
      "token": "a292d75bd7011cf255a1bf236b23d0a5",
      "gateway": "manual",
      "test": false,
      "total_price": "6.99",
      "subtotal_price": "6.99",
      "total_weight": 0,
      "total_tax": "0.00",
      "taxes_included": false,
      "currency": "GBP",
      "financial_status": "pending",
      "confirmed": true,
      "total_discounts": "0.00",
      "total_line_items_price": "6.99",
      "cart_token": null,
      "buyer_accepts_marketing": true,
      "name": "#MW1051",
      "referring_site": null,
      "landing_site": null,
      "cancelled_at": null,
      "cancel_reason": null,
      "total_price_usd": "9.00",
      "checkout_token": null,
      "reference": null,
      "user_id": 1706983449,
      "location_id": 1327759385,
      "source_identifier": null,
      "source_url": null,
      "processed_at": "2018-08-09T22:18:53+01:00",
      "device_id": null,
      "phone": null,
      "customer_locale": null,
      "app_id": 1354745,
      "browser_ip": null,
      "landing_site_ref": null,
      "order_number": 1051,
      "discount_applications": [],
      "discount_codes": [],
      "note_attributes": [],
      "payment_gateway_names": [
        "manual"
      ],
      "processing_method": "manual",
      "checkout_id": null,
      "source_name": "shopify_draft_order",
      "fulfillment_status": null,
      "tax_lines": [],
      "tags": "",
      "contact_email": "james@bungeedesign.com",
      "order_status_url": "https://checkout.shopify.com/1245839385/orders/a292d75bd7011cf255a1bf236b23d0a5/authenticate?key=9322877ce6ce34be2feeb127d73d0f89",
      "admin_graphql_api_id": "gid://shopify/Order/532977778742",
      "line_items": [
        {
          "id": 1350552453174,
          "variant_id": 8725905408054,
          "title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
          "quantity": 1,
          "price": "6.99",
          "sku": "ss11",
          "variant_title": "S / Black",
          "vendor": "Fruit Of The Loom",
          "fulfillment_service": "manual",
          "product_id": 719146287158,
          "requires_shipping": true,
          "taxable": false,
          "gift_card": false,
          "name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Black",
          "variant_inventory_management": "shopify",
          "properties": [],
          "product_exists": true,
          "fulfillable_quantity": 1,
          "grams": 0,
          "total_discount": "0.00",
          "fulfillment_status": null,
          "discount_allocations": [],
          "admin_graphql_api_id": "gid://shopify/LineItem/1350552453174",
          "tax_lines": [
            {
              "title": "VAT",
              "price": "0.00",
              "rate": 0.2
            }
          ]
        }
      ],
      "shipping_lines": [],
      "fulfillments": [],
      "refunds": [],
      "customer": {
        "id": 552537620534,
        "email": "james@bungeedesign.com",
        "accepts_marketing": true,
        "created_at": "2018-06-15T10:44:13+01:00",
        "updated_at": "2018-08-09T22:18:53+01:00",
        "first_name": "James",
        "last_name": "Rogers",
        "orders_count": 18,
        "state": "enabled",
        "total_spent": "0.00",
        "last_order_id": 532977778742,
        "note": null,
        "verified_email": true,
        "multipass_identifier": null,
        "tax_exempt": false,
        "phone": null,
        "tags": "password page, prospect",
        "last_order_name": "#MW1051",
        "admin_graphql_api_id": "gid://shopify/Customer/552537620534"
      }
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

您所拥有的一切运行良好。翻译中可能会丢失某些内容,或者.id处于另一个级别。我的猜测是您没有按照预期设置变量jsonOrderData

如果您console.log(jsonOrderData);,您会得到期望的结果吗?

这很好:

const json = {
  "orders": [
    {
      "id": 533078016054,
      "email": "email@gmail.com",
      "closed_at": null,
      "created_at": "2018-08-10T05:03:36+01:00",
      "updated_at": "2018-08-10T05:03:37+01:00",
      "number": 52,
      "note": "",
      "token": "f4877048c08eb98180ee5fda34f978bc",
      "gateway": "manual",
      "test": false,
      "total_price": "13.98",
      "subtotal_price": "13.98",
      "total_weight": 0,
      "total_tax": "0.00",
      "taxes_included": false,
      "currency": "GBP",
      "financial_status": "pending",
      "confirmed": true,
      "total_discounts": "0.00",
      "total_line_items_price": "13.98",
      "cart_token": null,
      "buyer_accepts_marketing": false,
      "name": "#MW1052",
      "referring_site": null,
      "landing_site": null,
      "cancelled_at": null,
      "cancel_reason": null,
      "total_price_usd": "18.00",
      "checkout_token": null,
      "reference": null,
      "user_id": 1706983449,
      "location_id": null,
      "source_identifier": null,
      "source_url": null,
      "processed_at": "2018-08-10T05:03:36+01:00",
      "device_id": null,
      "phone": null,
      "customer_locale": null,
      "app_id": 1354745,
      "browser_ip": null,
      "landing_site_ref": null,
      "order_number": 1052,
      "discount_applications": [],
      "discount_codes": [],
      "note_attributes": [],
      "payment_gateway_names": [
        "manual"
      ],
      "processing_method": "manual",
      "checkout_id": null,
      "source_name": "shopify_draft_order",
      "fulfillment_status": null,
      "tax_lines": [],
      "tags": "",
      "contact_email": "email@gmail.com",
      "order_status_url": "https://checkout.shopify.com/1245839385/orders/f4877048c08eb98180ee5fda34f978bc/authenticate?key=redacted",
      "admin_graphql_api_id": "gid://shopify/Order/redacted",
      "line_items": [
        {
          "id": 1350736445494,
          "variant_id": 8725905539126,
          "title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
          "quantity": 1,
          "price": "6.99",
          "sku": "ss15",
          "variant_title": "S / Bottle Green",
          "vendor": "Fruit Of The Loom",
          "fulfillment_service": "manual",
          "product_id": 719146287158,
          "requires_shipping": true,
          "taxable": false,
          "gift_card": false,
          "name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Bottle Green",
          "variant_inventory_management": "shopify",
          "properties": [],
          "product_exists": true,
          "fulfillable_quantity": 1,
          "grams": 0,
          "total_discount": "0.00",
          "fulfillment_status": null,
          "discount_allocations": [],
          "admin_graphql_api_id": "gid://shopify/LineItem/1350736445494",
          "tax_lines": [
            {
              "title": "VAT",
              "price": "0.00",
              "rate": 0.2
            }
          ]
        },
        {
          "id": 1350736478262,
          "variant_id": 8725905440822,
          "title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
          "quantity": 1,
          "price": "6.99",
          "sku": "ss12",
          "variant_title": "S / Heather Grey",
          "vendor": "Fruit Of The Loom",
          "fulfillment_service": "manual",
          "product_id": 719146287158,
          "requires_shipping": true,
          "taxable": false,
          "gift_card": false,
          "name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Heather Grey",
          "variant_inventory_management": "shopify",
          "properties": [],
          "product_exists": true,
          "fulfillable_quantity": 1,
          "grams": 0,
          "total_discount": "0.00",
          "fulfillment_status": null,
          "discount_allocations": [],
          "admin_graphql_api_id": "gid://shopify/LineItem/1350736478262",
          "tax_lines": [
            {
              "title": "VAT",
              "price": "0.00",
              "rate": 0.2
            }
          ]
        }
      ],
      "shipping_lines": [],
      "billing_address": {
        "first_name": "John",
        "address1": "17A Oaklands Business Centre",
        "phone": null,
        "city": "Worthing",
        "zip": "BN11 5LH",
        "province": null,
        "country": "United Kingdom",
        "last_name": "Doe",
        "address2": "Elm Grove",
        "company": null,
        "latitude": 50.8162744,
        "longitude": -0.4010653,
        "name": "Jogn Doe",
        "country_code": "GB",
        "province_code": null
      },
      "shipping_address": {
        "first_name": "John",
        "address1": "17A Oaklands Business Centre",
        "phone": null,
        "city": "Worthing",
        "zip": "BN11 5LH",
        "province": null,
        "country": "United Kingdom",
        "last_name": "Doe",
        "address2": "Elm Grove",
        "company": null,
        "latitude": 50.8162744,
        "longitude": -0.4010653,
        "name": "John Doe",
        "country_code": "GB",
        "province_code": null
      },
      "fulfillments": [],
      "refunds": [],
      "customer": {
        "id": 556974014518,
        "email": "email@gmail.com",
        "accepts_marketing": false,
        "created_at": "2018-06-26T00:26:55+01:00",
        "updated_at": "2018-08-10T05:03:36+01:00",
        "first_name": "John",
        "last_name": "Doe",
        "orders_count": 22,
        "state": "enabled",
        "total_spent": "0.00",
        "last_order_id": 533078016054,
        "note": null,
        "verified_email": true,
        "multipass_identifier": null,
        "tax_exempt": false,
        "phone": null,
        "tags": "",
        "last_order_name": "#MW1052",
        "admin_graphql_api_id": "gid://shopify/Customer/556974014518",
        "default_address": {
          "id": 601657278518,
          "customer_id": 556974014518,
          "first_name": "John",
          "last_name": "Doe",
          "company": null,
          "address1": "17A Oaklands Business Centre",
          "address2": "Elm Grove",
          "city": "Worthing",
          "province": null,
          "country": "United Kingdom",
          "zip": "BN11 5LH",
          "phone": null,
          "name": "John Doe",
          "province_code": null,
          "country_code": "GB",
          "country_name": "United Kingdom",
          "default": true
        }
      }
    },
    {
      "id": 532977778742,
      "email": "james@bungeedesign.com",
      "closed_at": null,
      "created_at": "2018-08-09T22:18:53+01:00",
      "updated_at": "2018-08-09T22:18:53+01:00",
      "number": 51,
      "note": "",
      "token": "a292d75bd7011cf255a1bf236b23d0a5",
      "gateway": "manual",
      "test": false,
      "total_price": "6.99",
      "subtotal_price": "6.99",
      "total_weight": 0,
      "total_tax": "0.00",
      "taxes_included": false,
      "currency": "GBP",
      "financial_status": "pending",
      "confirmed": true,
      "total_discounts": "0.00",
      "total_line_items_price": "6.99",
      "cart_token": null,
      "buyer_accepts_marketing": true,
      "name": "#MW1051",
      "referring_site": null,
      "landing_site": null,
      "cancelled_at": null,
      "cancel_reason": null,
      "total_price_usd": "9.00",
      "checkout_token": null,
      "reference": null,
      "user_id": 1706983449,
      "location_id": 1327759385,
      "source_identifier": null,
      "source_url": null,
      "processed_at": "2018-08-09T22:18:53+01:00",
      "device_id": null,
      "phone": null,
      "customer_locale": null,
      "app_id": 1354745,
      "browser_ip": null,
      "landing_site_ref": null,
      "order_number": 1051,
      "discount_applications": [],
      "discount_codes": [],
      "note_attributes": [],
      "payment_gateway_names": [
        "manual"
      ],
      "processing_method": "manual",
      "checkout_id": null,
      "source_name": "shopify_draft_order",
      "fulfillment_status": null,
      "tax_lines": [],
      "tags": "",
      "contact_email": "james@bungeedesign.com",
      "order_status_url": "https://checkout.shopify.com/1245839385/orders/a292d75bd7011cf255a1bf236b23d0a5/authenticate?key=9322877ce6ce34be2feeb127d73d0f89",
      "admin_graphql_api_id": "gid://shopify/Order/532977778742",
      "line_items": [
        {
          "id": 1350552453174,
          "variant_id": 8725905408054,
          "title": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt",
          "quantity": 1,
          "price": "6.99",
          "sku": "ss11",
          "variant_title": "S / Black",
          "vendor": "Fruit Of The Loom",
          "fulfillment_service": "manual",
          "product_id": 719146287158,
          "requires_shipping": true,
          "taxable": false,
          "gift_card": false,
          "name": "Fruit of the Loom Poly/Cotton Piqué Polo Shirt - S / Black",
          "variant_inventory_management": "shopify",
          "properties": [],
          "product_exists": true,
          "fulfillable_quantity": 1,
          "grams": 0,
          "total_discount": "0.00",
          "fulfillment_status": null,
          "discount_allocations": [],
          "admin_graphql_api_id": "gid://shopify/LineItem/1350552453174",
          "tax_lines": [
            {
              "title": "VAT",
              "price": "0.00",
              "rate": 0.2
            }
          ]
        }
      ],
      "shipping_lines": [],
      "fulfillments": [],
      "refunds": [],
      "customer": {
        "id": 552537620534,
        "email": "james@bungeedesign.com",
        "accepts_marketing": true,
        "created_at": "2018-06-15T10:44:13+01:00",
        "updated_at": "2018-08-09T22:18:53+01:00",
        "first_name": "James",
        "last_name": "Rogers",
        "orders_count": 18,
        "state": "enabled",
        "total_spent": "0.00",
        "last_order_id": 532977778742,
        "note": null,
        "verified_email": true,
        "multipass_identifier": null,
        "tax_exempt": false,
        "phone": null,
        "tags": "password page, prospect",
        "last_order_name": "#MW1051",
        "admin_graphql_api_id": "gid://shopify/Customer/552537620534"
      }
    }
  ]
};

for (let i = 0; i < json.orders.length; i++) {
    const order = json.orders[i];
    console.log(order.customer.id);
}