嵌套JavaScript数组的分隔列表

时间:2018-01-02 17:34:50

标签: javascript arrays

我有一个包含嵌套数组的简单JavaScript对象。 所有我在这里寻找我们返回以逗号分隔的" productID' " digitalData.transaction.item.productInfo [i] .productID [j]"

中出现的值

我很感激帮助。

这是JS的一个例子(抱歉长度,但我觉得比抱歉更好):

window.digitalData = {
  transaction: {
    purchaseID: "30010819", //Typically the same as transactionID
    transactionID: "30010819", //Unique Identifier for this transaction
    item: [{ //Array of items in this transaction
      productInfo: {
        productID: "63493",
        color: "Fuscia", //color if this product has colors
        size: "M (8-10)", //size if this product has sizes
        sku: "63493-12456", //sku
        inventoryStatus: "In Stock", // Mirror the on-site messaging
        skuAttr: [{ // extensible array of sku attributes and values
          typeLabel: "Color",
          valueCode: "FSA",
          valueLabel: "Fuscia"
        }, {
          typeLabel: "Size",
          valueLabel: "M (8-10)"
        }]
      },
      price: {
        currency: "USD"
      },
      quantity: 1
    }, {
      productInfo: {
        productID: "55027",
        color: "", //color if this product has colors
        size: "", //size if this product has sizes
        sku: "55027", //sku if known. otherwise use productID
        inventoryStatus: "Low Stock", // Mirror the on-site messaging
        skuAttr: [] //Empty array if no sku attributes exist
      },
      price: {
        currency: "USD"
      },
      quantity: 2     
      }]
    }]
  }

1 个答案:

答案 0 :(得分:2)

你可以这样做:

digitalData.transaction.item.map(x => x.productInfo.productID).join(',')