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?

贡献者指南

Stringifying using different arrayFormats, then parsed, create different things · ljharb/qs#345 | Good First Issue