我有一个数据库字段,在 firestore 中有一个这样的 map :
坐标:{_01:"铜",_02:"金",_03:"铁"}
我在 firestore 管理面板中看到这个数据库,如下所示: pic
当我尝试使用下面的代码列出项目时
data.cordinates.map((item, i)=>
console.log(i+" - "+item)
);
我收到:
Unhandled Rejection (TypeError): data.cordinates.map is not a function
读/写这种 map 的正确方法是什么?
顺便说一句,console.log(data.corords) 给我这样的输出:
{_01: "copper", _02: "gold", _03: "iron", _04: "", _05: "", …}
_01: "copper"
_02: "gold"
_03: "iron"
_04: ""
_05: ""
_06: ""
_07: ""
_08: ""
_10: ""
_11: ""
_12: ""
_13: ""
_14: ""
_15: ""
_16: ""
_17: ""
_18: ""
_20: ""
_21: ""
_22: ""
_23: ""
_24: ""
_25: ""
_26: ""
_27: ""
_28: ""
_30: ""
_31: ""
_32: ""
_33: ""
_34: ""
_35: ""
_36: ""
_37: ""
_38: ""
_40: ""
_41: ""
_42: ""
_43: ""
_44: ""
_45: ""
_46: ""
_47: ""
_48: ""
是的,总共有 48 个元素
感谢任何帮助,提前致谢。
最佳答案
将map
更改为forin
这里是示例代码:
const cord = data.cordinates || {} // to handle undefined `data.cordinates` case
for(let i in cord){
if(cord.hasOwnProperty(i)){
const item = cord[i];
console.log(i+" - "+item);
// do your other stuff here
}
}
关于javascript - 如何从 React 中读取和写入 firestore 中的 map 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58779623/