我需要一个地图来替换我的React应用程序中的一些内容。我有一个返回地图的NodeJS。
let output = new Map();
output.set("#propertyName#", property.PropertyName || "");
output.set("#propertyPhone#", property.PropertyPhone || "");
output.set("#propertyEmail#", property.PropertyEmail || "");
output.set("#propertyStreet#", property.PropertyAddress1 || "");
// ... I have a lot more of output.set. The property variable is a database query response
// Then, I return it as an array. I tried to return the map object and it returned an empty object {}
res.json([...output]);问题是它返回一个数组,而不是我的map。
[
[
"#propertyName#",
"ABC"
],
[
"#propertyPhone#",
"123"
],
[
"#propertyEmail#",
"None@none.com"
],
[
"#propertyStreet#",
"123 main"
]
]有没有办法在React中将这个数组转换回Map?或者更好的是,我可以从NodeJS返回地图吗?
谢谢
发布于 2021-02-04 04:08:23
从Node服务器向react应用程序发送数据使用JSON,在JSON中没有Map。幸运的是,您可以在创建映射时传入数组。
const myMap = new Map(serverData)
https://stackoverflow.com/questions/66034833
复制相似问题