如何替换为json生成器代码

时间:2017-03-24 13:39:18

标签: javascript jquery replace

我在Datalayer工作,我正在尝试替换价值。例如,如果item.listPrice等于0.01,则将打印为0.00,但无法正常工作。

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);
    }
}

2 个答案:

答案 0 :(得分: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);
  }
}

答案 1 :(得分: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);
    }
}