While attempting to convert an array-like object into a string using JSON.stringify, I discovered that the function was not working correctly when the array-like object was declared as an array object.
For more clarity, please refer to the following --> jsFiddle
var simpleArray = []; //Note that it is defined as an Array Object
alert(typeof simpleArray); //Returns object -> Array Object
simpleArray ['test1'] = 'test 1';
simpleArray ['test2'] = 'test 2';
alert(JSON.stringify(simpleArray)); //Returns []
When I changed
var simpleArray = [];
to var simpleArray = {};
, it worked correctly and provided me with
{"test1":"test 1","test2":"test 2"}
.
Could someone please provide some insight or a reference where I can find more information on this topic?
Edit:
Question: Why did JSON.stringify fail to return
{"test1":"test 1","test2":"test 2"}
when typeof simpleArray = []
and simpleArray = {}
both returned object?