3

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 ':'

10
  • You're using new syntax features in an old browser. This is what transpilers are built to handle. Commented Mar 24, 2017 at 17:28
  • I could be blind but I don't see any ES2015 in his code. Commented Mar 24, 2017 at 17:31
  • 1
    @ChipDean: Look more closely. Commented Mar 24, 2017 at 17:33
  • 1
    Ah good catch :) Commented Mar 24, 2017 at 17:33
  • 1
    @denisb: If data is an Array, you can do this more cleanly with .map()... var temp2 = data.map(function(o) { return {vproductid:o.productid, vproductname:o.product_name, vexpirydt:o.expiry} }) Commented Mar 24, 2017 at 17:37

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much! Only minor tweak is to return temp2 within square brackets for my own display purposes, but it works!! Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.