In this scenario, you start by initializing an array item = []
and then assigning string properties to it.
However, JSON.stringify
assumes that an array should have numeric properties and therefore does not attempt to iterate over non-numeric properties.
A better solution would be to use an object instead {}
Here's an excerpt from the specification:
If Type(value) is Object, and IsCallable(value) is false
If the [[Class]] internal property of value is "Array" then
Return the result of calling the abstract operation JA with argument value.
followed by
Let len be the result of calling the [[Get]] internal method of value with argument "length".
Let index be 0.
Repeat while index < len
Let strP be the result of calling the abstract operation Str with arguments ToString(index) and value.
If strP is undefined
Append "null" to partial.
Else
Append strP to partial.
Increment index by 1.
References:
- 15.12.3 stringify
- 15.12.3 stringify JA