2017-03-24 43 views
0

我在Datalayer中工作,我試圖做一個替換值。例如,如果item.listPrice等於0.01,這將打印0.00,但不能正常工作。如何做到替換成json發生器代碼

function get_products() { 
 
    try { 
 
     vtexjs.checkout.getOrderForm().done(function(a) { 
 
      console.log("Push order", a); 
 
      arr_products_cart = []; 
 
      $(a.items).each(function(a, b) { 
 
       arr_products_cart.push({ 
 
        name: b.name, 
 
        id: b.id, 
 
        price: regex_price(b.listPrice), 
 
        brand: brand, 
 
        category: b.productCategories[Object.keys(b.productCategories)[1]], 
 
        variant: b.skuName 
 
       }); 
 
       if ("0,01" === b.listPrice || "0.01" === b.listPrice) arr_products_cart.push({ 
 
        price: "0.00" 
 
       }); 
 
       arr_products_cart_category.push({ 
 
        id: b.id, 
 
        category: b.productCategories[Object.keys(b.productCategories)[1]], 
 
        variant: b.skuName 
 
       }); 
 
      }); 
 
      set_products_cart(); 
 
      cart_qty_register_category(); 
 
     }); 
 
    } catch (a) { 
 
     console.warn("get_products error", a); 
 
    } 
 
}

回答

0

正確的方法:

function get_products() { 
 
    try { 
 
     vtexjs.checkout.getOrderForm().done(function(a) { 
 
      console.log("Push order", a); 
 
      arr_products_cart = []; 
 
      $(a.items).each(function(a, b) { 
 
       var value = format_price(item.listPrice) == '0.01' ? '0.00' : format_price(item.listPrice); 
 
       arr_products_cart.push({ 
 
        name: b.name, 
 
        id: b.id, 
 
        price: value, 
 
        brand: brand, 
 
        category: b.productCategories[Object.keys(b.productCategories)[1]], 
 
        variant: b.skuName 
 
       }); 
 
       arr_products_cart_category.push({ 
 
        id: b.id, 
 
        category: b.productCategories[Object.keys(b.productCategories)[1]], 
 
        variant: b.skuName 
 
       }); 
 
      }); 
 
      set_products_cart(); 
 
      cart_qty_register_category(); 
 
     }); 
 
    } catch (a) { 
 
     console.warn("get_products error", a); 
 
    } 
 
}

0

只是不要同時推動數據到陣列如下面的替換。

function get_products() { 
    try { 
    vtexjs.checkout.getOrderForm().done(function(a) { 
     console.log("Push order", a); 
     arr_products_cart = []; 
     $(a.items).each(function(a, b) { 
     arr_products_cart.push({ 
      name: b.name, 
      id: b.id, 
      price: if ("0,01" === b.listPrice || "0.01" === b.listPrice) 
      "0.00" 
      else 
      b.listPrice 
      brand: brand, 
      category: b.productCategories[Object.keys(b.productCategories)[1]], 
      variant: b.skuName 
     }); 
     arr_products_cart_category.push({ 
      id: b.id, 
      category: b.productCategories[Object.keys(b.productCategories)[1]], 
      variant: b.skuName 
     }); 
     }); 
     set_products_cart(); 
     cart_qty_register_category(); 
    }); 
    } catch (a) { 
    console.warn("get_products error", a); 
    } 
} 
+0

使用該溶液是顯示一個消息 「未捕獲的SyntaxError:意外令牌如果」 –