You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React中qs.stringify()函数如何保留空数组键?

解决qs.stringify()保留空数组键的问题

要让qs序列化时保留空数组对应的键,给qs.stringify()传入配置选项{ allowEmptyArrays: true }即可。

修改后的代码如下:

const data = {
  key: [],
  name: "Lower"
};

const stringifiedData = qs.stringify(data, { allowEmptyArrays: true });
console.log(stringifiedData); // 输出:key=[]&name=Lower

默认情况下qs会忽略空数组,不会生成对应的键值对;开启allowEmptyArrays: true后,空数组会被序列化为key=[]的形式,完全匹配你的期望结果。

内容的提问来源于stack exchange,提问作者Vishal Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 11:37:14