Javascript-数组内循环

时间:2018-08-20 22:36:39

标签: javascript

我知道这将是一个简单的过程,并且我尝试将堆栈溢出中的不同拼图拼合在一起,但是我大脑的某些部分却无法正常工作。

我正在编写一个Web收集JavaScript,它应该通过数据层解析我的购物车详细信息。

var cartcontent = [{
"item": tealiumStaticImpressions.cart.product_sku.toString().split(".", 2)[0] + '.' + tealiumStaticImpressions.cart.product_sku.toString().split(".", 2)[1],
"quantity": parseInt(tealiumStaticImpressions.cart.product_quantity),
"price": parseFloat(tealiumStaticImpressions.cart.cart_product_price)
}];

我需要“项目”,“数量”和“价格”以适用于所有项目-目前仅提取第一个项目。

我发现这是部分匹配

for (var key in p.cart_product_sku) {
    if (p.cart_product_sku.hasOwnProperty(key)) {
        console.log("item" + " -> " + p.cart_product_sku[key]);
    }
}

以下是预期结果

 {item: 'item_1', price: 19.9, quantity: 1},
 {item: 'item_2', price: 29.7, quantity: 3}

但是不确定它们如何协同工作

编辑 这是tealiumStaticImpressions的内容

tealiumStaticImpressions.cart {
    cart_product_id: Array(2),
    cart_product_price: Array(2),
    cart_product_quantity: Array(2),
    cart_product_sku: Array(2),
    cart_total_items: "2",
     …
}
cart_product_id: Array(2) 0: "J181LS81.NVY.10"
1: "J173SS202.MUS.3"
length: 2 __proto__: Array(0) cart_product_price: Array(2) 
0: "81.81"
1: "72.72"
length: 2 __proto__: Array(0) cart_product_quantity: Array(2) 
0: "1"
1: "1"
length: 2 __proto__: Array(0) cart_product_sku: Array(2) 
0: "J181LS81.NVY.10"
1: "J173SS202.MUS.3"
length: 2 __proto__: Array(0) cart_total_items: "2"
cart_total_value: "169.98"
checkout_step: "1"
ecommerce_action: "checkout"
page_type: "cart"
product_category: (2) ["Long Sleeve Shirts", "Short Sleeve Shirts"] product_name: (2) ["DONOVAN CHECK SHIRT", "HENRI LINEN CHECK SHIRT"] product_price: (2) ["89.99", "79.99"] product_quantity: (2) ["1", "1"] product_size: (2) ["LT", "4XL"] product_sku: (2) ["J181LS81.NVY.10", "J173SS202.MUS.3"] product_subcategory: (2) ["Shirts", "Shirts"] __proto__: Object

1 个答案:

答案 0 :(得分:0)

是的,我认为这很容易做到。我们所做的基本假设是

  1. cart中的任何可迭代属性(可以像数组一样迭代的属性)与其他可迭代属性的索引对应。也就是说,cart_product_id[0]对应于cart_product_price[0]cart_product_quantity[0]等。

  2. 我们新集合的属性名称可以来自cart道具本身的名称。因此,采用以下道具:cart_product_idcart_product_pricecart_product_quantity等,我们的对象看起来像:

    {
      id: 0,
      price: "$0.10",
      quantity: 10
      ... 
    } 
    

这些假设将使我们创建一个循环,该循环首先遍历我们期望的产品总数,然后遍历cart中的属性。第二个循环将提供新的项目集合,一次一次。

const cart = tealiumStaticImpressions.cart;
const totalItems = parseInt(cart.cart_total_items);
// Some of the properties in `cart` don't provide item information. We'll exclude them
const exclude = ['cart_total_items']; 

let collection = [];

for (let i =0; i < totalItems; i++) {
  let currentItem = {};

  Object.keys(cart).forEach(key => {  // Loop through only own properties in cart
    if (exclude.some(ex => ex === key)) return; // Don't process keys in cart that are in the exclude list

    let prop = key.split("_").pop(); // Get the word after the last "_"

    currentItem[prop] = cart[key][i]; // Map the value of this property to our new collection item
  });

  // We populated our current item, now push into the collection
  collection.push(currentItem); 
}