ljharb/qs

Stringifying using different arrayFormats, then parsed, create different things

Open

#345 建立於 2019年11月15日

在 GitHub 查看
 (16 留言) (0 反應) (0 負責人)JavaScript (8,015 star) (744 fork)batch import
bughelp wantedparsequestionstringify

描述

It seems stringifying my metadata object with arrayFormat: brackets, then parsing it, creates a different object to stringifying with arrayFormat: indices then parsing that string.

> const qs = require('qs');

> metadata = [{key: "a", value: "2"}, {key: "b", value: "3"}]
[ { key: 'a', value: '2' }, { key: 'b', value: '3' } ]

> brackets = qs.stringify({"metadata": metadata}, { arrayFormat: "brackets"})
'metadata%5B%5D%5Bkey%5D=a&metadata%5B%5D%5Bvalue%5D=2&metadata%5B%5D%5Bkey%5D=b&metadata%5B%5D%5Bvalue%5D=3'

> parsed_brackets = qs.parse(brackets)
{ metadata: [ { key: [Array], value: [Array] } ] }

> parsed_brackets["metadata"]
[ { key: [ 'a', 'b' ], value: [ '2', '3' ] } ]

> indices = qs.stringify({"metadata": metadata}, { arrayFormat: "indices"})
'metadata%5B0%5D%5Bkey%5D=a&metadata%5B0%5D%5Bvalue%5D=2&metadata%5B1%5D%5Bkey%5D=b&metadata%5B1%5D%5Bvalue%5D=3'

> parsed_indices = qs.parse(indices)
{ metadata: [ { key: 'a', value: '2' }, { key: 'b', value: '3' } ] }

Am I doing something wrong? Is this expected? Is this just an issue with stringifying/parsing nested objects?

貢獻者指南