0

I'm struggling on adding an Object to an Array (E-commerce context).

My "tc_vars" datalayer is mapped with another datalayer which is called "wa_data". The latter sends the requested information to the first one.

An Object in that case will be a specific product and the Array will be the cart.content property :

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

}

The problem I'm facing here is that my code is not creating a new object for each new product that is added to the cart content (with all the dedicated properties of the new object).

I've tried using a loop which scans my cart content Array but apparently it's not working (not adding a new object inside the Array). Seems like I'm missing something.

Do you guys have any ideas?

Thx a lot

J

Jordan
  • 25
  • 5

3 Answers3

3

tc_vars.cart.content[i] is undefined. You need to define it first, before filling it up.

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
    // ....
}

As an alternative (lighter syntax and faster execution), you could also write :

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,
          // ....
    }
}

But we don't usually add things to an Array by its index. We just push things into it :

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,
          // ....
    });
}

This being said, it looks like all you're doing here is copying (not instanciating) wa_data.cart.content into tc_vars.cart.content. So you can completely forget my answer and replace your whole for loop with Gurvinder's answer (+1'd him):

tc_vars.cart.content = JSON.parse(JSON.stringify(wa_data.cart.content));
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
  • Thx a lot Jeremy for your help, I tried all your solutions and apparently, only the last one works correctly. The first two give me the same result as before. The 3 alternative gives me the objects needed but there's always one empty object that appears. – Jordan May 03 '16 at 12:44
1

Unless wa_data already have objects repeated at all the index, following code should work

tc_vars.cart.content = JSON.parse(JSON.stringify(wa_data.cart.content));
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

You can use an object literal:

tc_vars.cart.content[i] = {

    productID: wa_data.cart.content[i].productID,
    name: wa_data.cart.content[i].name,
    quantity: wa_data.cart.content[i].quantity,
    promoCode: wa_data.cart.content[i].promoCode,
    offerID: wa_data.cart.content[i].offerID,
    salesType: wa_data.cart.content[i].salesType,
    platform: wa_data.cart.content[i].platform,
    unitSalePrice: wa_data.cart.content[i].unitSalePrice,
    salePrice: wa_data.cart.content[i].salePrice,
    stockAvailability: wa_data.cart.content[i].stockAvailability,
    lineItemTotalAmountDiscounted: wa_data.cart.content[i].lineItemTotalAmountDiscounted,
    lineItemTotalAmountNotDiscounted: wa_data.cart.content[i].lineItemTotalAmountNotDiscounted,
    lineItemTaxAmount: wa_data.cart.content[i].lineItemTaxAmount,
    lineItemDiscountedAmount: wa_data.cart.content[i].lineItemDiscountedAmount,
    lineItemShippingCost: wa_data.cart.content[i].lineItemShippingCost,
    crossSell: wa_data.cart.content[i].crossSell,
    upSell: wa_data.cart.content[i].upSell

}
stefanhorne
  • 1,619
  • 3
  • 16
  • 23