jquery-deparam icon indicating copy to clipboard operation
jquery-deparam copied to clipboard

round tripping an object whose first keys are numeric does not work

Open binki opened this issue 5 years ago • 0 comments

> jQuery.param({a:{'0':1,x:4}})
'a%5B0%5D=1&a%5Bx%5D=4'
> jqueryDeparam(jQuery.param(jqueryDeparam(jQuery.param({a:{'0':1,x:4}}))))
{ a: [ '1' ] }
> jqueryDeparam(jQuery.param(jqueryDeparam(jQuery.param({a:{'0':1,x:4}})))).a.x
undefined
> jqueryDeparam(jQuery.param({a:{'0':1,x:4}})).a.x
'4'
> typeof jqueryDeparam(jQuery.param({a:{'0':1,x:4}})).a
'object'
> jQuery.isArray(jqueryDeparam(jQuery.param({a:{'0':1,x:4}})).a)
true

An object with numeric keys is encoded in the same way as an array by jQuery.param(). jQuery.param() will, like JSON.stringify(), ignore any string keys on an array object when serializing. However, this package, jquery-deparam assumes that an object is an array if the first key name it encounters is numeric.

jquery-param treats things as arrays sometims when it shouldn’t. As a result, the transform becomes lossy.

binki avatar Aug 21 '20 20:08 binki