将对象添加到数组

时间:2016-05-03 10:21:59

标签: javascript arrays object data-layers

我正在努力将一个Object添加到一个数组(电子商务上下文)。

我的“tc_vars”数据层与另一个名为“wa_data”的数据层映射。后者将请求的信息发送给第一个。

在这种情况下,对象将是特定产品,而Array将是cart.content属性:

var tc_vars = {

    nav : {
            siteCategory : wa_data.nav.siteCategory,
            environment :wa_data.nav.environment,
            siteType :wa_data.nav.siteType,
            siteName :wa_data.nav.siteName,
            pageName :wa_data.nav.pageName,
            siteSection :wa_data.nav.siteSection,
            country :wa_data.nav.country,
            language :wa_data.nav.language,
            template :wa_data.nav.template,
            doNotTrack :window.navigator.doNotTrack,
            customReferrer :wa_data.nav.customReferrer,
            genomeID :wa_data.nav.genomeID,
            mdmBID :wa_data.nav.mdmBID,
            mdmIID :wa_data.nav.mdmIID
        },

    profile : {
            uplayID : readCookie("user_id"),
            loginStatus : ''
        },

    internalSearch : {
            searchStatus :wa_data.internalSearch.searchStatus,
            searchFilters :wa_data.internalSearch.searchFilters,
            searchKeyWord :wa_data.internalSearch.searchKeyWord,
            totalResults :wa_data.internalSearch.totalResults,
            resultPosition :wa_data.internalSearch.resultPosition,
            autoCompletion :wa_data.internalSearch.autoCompletion
        },

    product : {
            productID :wa_data.product.productID,
            unitSalePrice :wa_data.product.unitSalePrice,
            salePrice :wa_data.product.salePrice,
            stockAvailability :wa_data.product.stockAvailability,
            salesType :wa_data.product.salesType,
            costOfGood :wa_data.product.costOfGood
        },    

    cart : {
            orderID:wa_data.cart.orderID,
            cartOpen:wa_data.cart.cartOpen,
            cartAdd:wa_data.cart.cartAdd,
            cartRemove:wa_data.cart.cartRemove,
            cartView:wa_data.cart.cartView,
            checkout:wa_data.cart.checkout,
            purchase:wa_data.cart.purchase,
            currency:wa_data.cart.currency,
            paymentMethod:wa_data.cart.paymentMethod,
            orderShipping:wa_data.cart.orderShipping,
            orderTotalAmountDiscounted:wa_data.cart.orderTotalAmountDiscounted,
            orderTotalAmountNotDiscounted:wa_data.cart.orderTotalAmountNotDiscounted,
            orderTaxAmount:wa_data.cart.orderTaxAmount,
            orderDiscountedAmount:wa_data.cart.orderDiscountedAmount,
            orderShippingCost:wa_data.cart.orderShippingCost,
            billingRegion:wa_data.cart.billingRegion,
            billingCity:wa_data.cart.billingCity,
            orderStatus:wa_data.cart.orderStatus,
            content : [{
                    productID:'',
                    name:'',
                    quantity :'',
                    promoCode:'',
                    offerID:'',
                    salesType:'',
                    platform :'',
                    unitSalePrice:'',
                    salePrice:'',
                    stockAvailability:'',
                    lineItemTotalAmountDiscounted:'',
                    lineItemTotalAmountNotDiscounted:'',
                    lineItemTaxAmount:'',
                    lineItemDiscountedAmount:'',
                    lineItemShippingCost:'',
                    crossSell:'',
                    upSell:''
                      }]
                },

    tech : {
            containerVersion : wa_data.tech.containerVersion
    }    

}

            //Scanning for the content using a loop
  if (typeof tc_vars.cart.content !== 'undefined' && tc_vars.nav.pageName === 'Basket'){
for(i=0; i < tc_vars.cart.content.length; i++) {
    tc_vars.cart.content[i].productID = wa_data.cart.content[i].productID;
    tc_vars.cart.content[i].name = wa_data.cart.content[i].name;
    tc_vars.cart.content[i].quantity = wa_data.cart.content[i].quantity;
    tc_vars.cart.content[i].promoCode = wa_data.cart.content[i].promoCode;
    tc_vars.cart.content[i].offerID = wa_data.cart.content[i].offerID;
    tc_vars.cart.content[i].salesType = wa_data.cart.content[i].salesType;
    tc_vars.cart.content[i].platform = wa_data.cart.content[i].platform;
    tc_vars.cart.content[i].unitSalePrice = wa_data.cart.content[i].unitSalePrice;
    tc_vars.cart.content[i].salePrice = wa_data.cart.content[i].salePrice;
    tc_vars.cart.content[i].stockAvailability = wa_data.cart.content[i].stockAvailability;
    tc_vars.cart.content[i].lineItemTotalAmountDiscounted = wa_data.cart.content[i].lineItemTotalAmountDiscounted;
    tc_vars.cart.content[i].lineItemTotalAmountNotDiscounted = wa_data.cart.content[i].lineItemTotalAmountNotDiscounted;
    tc_vars.cart.content[i].lineItemTaxAmount = wa_data.cart.content[i].lineItemTaxAmount;
    tc_vars.cart.content[i].lineItemDiscountedAmount = wa_data.cart.content[i].lineItemDiscountedAmount;
    tc_vars.cart.content[i].lineItemShippingCost = wa_data.cart.content[i].lineItemShippingCost;
    tc_vars.cart.content[i].crossSell = wa_data.cart.content[i].crossSell;
    tc_vars.cart.content[i].upSell = wa_data.cart.content[i].upSell;
}

}

我在这里遇到的问题是我的代码没有为添加到购物车内容的每个新产品创建一个新对象(具有新对象的所有专用属性)。

我尝试过使用一个循环来扫描我的购物车内容数组,但显然它不起作用(不在数组中添加新对象)。好像我错过了一些东西。

你们有什么想法吗?

很多

Ĵ

3 个答案:

答案 0 :(得分:3)

tc_vars.cart.content [i]未定义。在填写之前,您需要先定义它。

for(i=0; i < tc_vars.cart.content.length; i++) {
    tc_vars.cart.content[i] = {}; // Creates an empty object
    tc_vars.cart.content[i].productID = wa_data.cart.content[i].productID; // Starts filling it
    // ....
}

作为替代方案(更轻的语法和更快的执行),您还可以编写:

for(i=0; i < tc_vars.cart.content.length; i++) {
    tc_vars.cart.content[i] = {
          productID : wa_data.cart.content[i].productID,
          name      : wa_data.cart.content[i].name,
          // ....
    }
}

但我们通常不会通过索引向Array添加内容。我们只是把事情推进去了:

for(i=0; i < tc_vars.cart.content.length; i++) {
    tc_vars.cart.content.push({
          productID : wa_data.cart.content[i].productID,
          name      : wa_data.cart.content[i].name,
          // ....
    });
}

话虽如此,看起来你在这里所做的只是将wa_data.cart.content复制({1}}到tc_vars.cart.content。因此,您可以完全忘记我的答案,并用Gurvinder的答案替换整个for循环(+ 1他):

tc_vars.cart.content = JSON.parse(JSON.stringify(wa_data.cart.content));

答案 1 :(得分:1)

除非wa_data已经在所有索引上重复了对象,否则以下代码应该可以正常工作

tc_vars.cart.content = JSON.parse(JSON.stringify(wa_data.cart.content));

答案 2 :(得分:0)

您可以使用对象文字:

tableView: heightForRowAtIndexPath: