denisb
denisb

Reputation: 787

JS array push error in IE only

I have the following script, which essentially extracts each value from the "data" array being passed and returns json value.

 function parsejson(data) {
           var temp2 = new Array();

           if (data) {
               $.each(data, function (i, val) {
                   vproductid = data[i].productid;
                   vproductname = data[i].product_name;
                   vexpirydt = data[i].expiry;

                   temp2.push({vproductid,vproductname,vexpirydt});

               }); 

               console.log([temp2]);
               return [temp2];
           }
       }

So in this case "data" within my console log comes back as:

Array [ Object, Object ]

Which contains the following objects -> values, etc, and in some instances my "expiry" date value is blank space, but still appears to work properly in Firefox and Chrome.

Array Details

The issues seems to be exclusively with IE 11 in my case.... I keep getting the following Error in IE only which ties back to the "push" to append to my array somehow. I don't know if it's a syntax error or the way in which I'm trying to append to my array, but obviously I'm doing something wrong. My intention is simply to return a second array in json format, so might be a simpler way.

SCRIPT1003: Expected ':'

Upvotes: 3

Views: 2988

Answers (1)

M.Octavio
M.Octavio

Reputation: 1808

Comments provide the answear to IE error, here is a sample code that will work on IE and Chrome:

function parsejson(data) {
    var temp2 = []; // this syntax might be prefered to create a new array

    if (data) {
        temp2 = data.map(function(element) { 
            return {
                vproductid: element.productid,
                vproductname: element.product_name,
                vexpirydt: element.expiry
            };
        });
    }
    console.log(temp2);
    return temp2;
}

var sampleData = [{
    productid: 1,
    product_name: 'a',
    expiry: 'Today',
    someThingelse: '',
}, {
    productid: 2,
    product_name: 'b',
    expiry: 'Today',
    someThingelse: '',
}, {
    productid: 3,
    product_name: 'c',
    expiry: 'Today',
    someThingelse: '',
}];


parsejson(sampleData);

Upvotes: 2

Related Questions