Help get this topic noticed by sharing it on Twitter, Facebook, or email.

Why does IE convert Array into JSON Object automatically ? (but not FF/CH)

Hello,

(windows 7 / IE 11)

I have spent some days to try and understand an issue I met with AJAX/JSON/DB ASYNC.
On Firefox + Chrome : OK
On IE : KO :/

NB : I have done a kind of unit test (staging 73770).

I explain :

1. I execute an "appAPI.request.post()" with "responseDataType: 'application/json'"
2. I got a JSON response (from PHP script which uses a json_encode) which is an array : [["a"],["b"],["c"]]

Then I do a serie of tests on this "response" (myArray) :

A. console.log(myArray) : [["a"],["b"],["c"]] on all browsers. IE prefixes "[Object Array], everything is OK

B. typeof myArray : "Array" for FF/CH, "object" for IE.

C. appAPI.utils.isArray(myArray) : true for FF/CH, false for IE.

D. myArray instanceof Array : true for FF/CH, false for IE.

E. myArray.constructor : function Array() { [native code] } for ALL browsers.

F. myArray.constructor === Array : true for FF/CH, false for IE.

G. Array.isArray(myArray) : true for FF/CH, not implemented for IE.

H. myArray.length : good count (3) for ALL browsers.

I : for (i = 0, j = myArray.length; i < j; i++) {} : works for ALL browsers, even IE.

So, IE looks to NOT recognize that is an Array but is able to dump it correctly (as a [Object Array]), to parse it (instruction for) and to get its length.

3. After that, I save it into DB : appAPI.db.async.set('myArray', myArray); // that works !

4. I retrieve it after some seconds : appAPI.db.async.get('myArray', function(myArrayInDb){}); then I do all the SAME tests in this callback function. In this case, with FF/CH I got the same result than above (everything is OK, it is an array) but on IE some results have changed :

A. console.log(myArrayInDb) : [["a"],["b"],["c"]] on FF/CH but on IE I get :
[Object Object] {0: Object {...}, 1: Object {...}, 3: Object {...}}

Not considered at all as an array, just an object, without length, not parsable in instruction "for", etc.

Have you ever seen this behavior ? I do not understand how to make IE consider this JSON Array as... an Array.
I thought about .stringify() / parse() but no way to keep it as Array (if I appAPI.JSON.stringify the response for saving it in the DB then IE saved : "{0: ...}" )

Thanks in advance
1 person has
this problem
+1
Reply
  • Another impressive behavior is when I get from DB in IE the [Object object] then try and convert it into array like this :

    var newArray = [];
    for (var i in myArrayInDb) {
    newArray.push(myArrayInDb[i]);
    }

    newArray is still an [Object object] and not an array.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited

  • Shlomo (Official Rep) April 12, 2015 08:45
    Hello eureka,

    Have you seen my response on the following thread which deals with a similar matter? js array methods erroring on IE
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited

  • Hello Shlomo,

    I have not thought it would be really related, because I do not use methods like "map" or "reduce" or something else.

    But if it is the case, as you seems to mean, that means than arrays are not at all arrays ? No "length" property ? Auto-conversion into Object when saving into DB or manipulating ? Etcaetera ?
    No way to use Arrays into background/popup scope ?

    According to your answer, what could be the alternative ? I need to store an array in local db, to retrieve it from popup and extension scopes later (too large to be passed through the appAPI.message).

    I need to finish one extension, which works like a charm under FF/CH and is totally buggy under IE.
    I need to find a solution. Thanks for your reply.

    Sincerely.
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited

  • According to the fact that some Array methods are not implemented, I used this way to convert the "IE Array which is not an Array" into Array :

    var newArray = [];
    for (var i in myArray) {
    newArray[i] = myArray[i];
    }
    In this way I can save into DB the newArray, which is recognized as an Array in IE 11.

    But the question stays : why an array extracted from the response of the "appAPI.request.post()" is not considered as an array from start ?

    Thanks.
    • Shlomo (Official Rep) April 14, 2015 07:28
      Thanks for the additional information. Your solution for converting the response array to a native array seems reasonable.

      As for why the response array is not native array, I have not investigated the issue but I think it has something to do with how IE handles POST requests. I have forwarded the issue to the dev teams queue for them to investigate, though it may be a while before they get to it,
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited