【发布时间】:2019-05-29 23:42:29
【问题描述】:
如何将 PostgreSQL 数组转换为字符串,例如。 "{1,2,3,4}" 到 javascript/typescript 数组:[1,2,3,4]。
数组中的值必须是数字类型。
我尝试了使用 replace 和 split 的解决方案,但它返回字符串值。
var test = "{1,2,3,4}";
test = test.replace("{", "");
test = test.replace("}", "");
//test = test.replace(/\{|\}/gm, "") //regex replace
test.split(",") //['1', '2', '3', '4']
【问题讨论】:
-
您不应该这样做。 postgresql 驱动程序应该为您完成。你用的是哪一个?
-
你可以用方括号替换大括号,然后使用 JSON.parse 方法。像这样
JSON.parse(test.replace("{", "[").replace("}", "]")) -
@DenysSéguret 我无权访问整个后端的功能。
-
@shajji 我知道,我只是想展示我正在做的所有步骤。
-
修复后端可能是正确的解决方案,但我知道并非所有项目都得到理想管理...
标签: javascript arrays string typescript type-conversion