如何循环并返回javascript对象的值?

如何循环并返回javascript对象的值?,javascript,reactjs,firebase,google-cloud-firestore,react-map-gl,Javascript,Reactjs,Firebase,Google Cloud Firestore,React Map Gl,(如果我的某些条款不正确,请道歉) 在Firebase我有很多帖子。每个帖子都有一个“纬度”字段和一个“经度”字段。我正在将它们取出并存储在一个名为mapRefs的数组/对象中: useEffect(() => { projectFirestore.collection("posts").get().then(res => { let mapRefs = []; res.forEach(data => {

(如果我的某些条款不正确,请道歉)

在Firebase我有很多帖子。每个帖子都有一个“纬度”字段和一个“经度”字段。我正在将它们取出并存储在一个名为mapRefs的数组/对象中:

useEffect(() => {
    projectFirestore.collection("posts").get().then(res => {
        let mapRefs = [];
        res.forEach(data => {
            mapRefs.push([data.data().myLatitude, data.data().myLongitude]);    
        });
        console.log(mapRefs);

        });
}, []);
这样,控制台日志的输出为:

0: (2) [-8.6848548, 115.22303799999999]
1: (2) [-8.7848548, 115.323038]
2: (2) [-8.9848548, 115.52303799999999]
3: (2) [-8.8848548, 115.42303799999999]
然后,我如何迭代这些并将纬度和经度值映射到组件。我试着这样做:

<ReactMapGL>
    { mapRefs && mapRefs.map(coord => (
        <Marker latitude={coord[0]} longitude={coord[1]}>
            <div>
                ...
            </div>
        </Marker>
    ))}
</ReactMapGL>

{mapRefs&&mapRefs.map(coord=>(
...
))}
这不管用。请说明正确的方法是什么?

您需要使用来呈现UI元素,并且
mapRefs
useffect
之外不可用

像这样试试

const [mapRefs, setMapRefs] = useState([])

useEffect(() => {
    projectFirestore.collection("posts").get().then(res => {
       let refs = [];
       res.forEach(data => {
          refs.push([data.data().myLatitude, data.data().myLongitude]);    
       });
       setMapRefs(refs)
    });
}, []);

return (
  <ReactMapGL>
    { mapRefs.map(coord => (
        <Marker latitude={coord[0]} longitude={coord[1]}>
            <div>
                ...
            </div>
        </Marker>
    ))}
</ReactMapGL>
)
const[mapRefs,setMapRefs]=useState([]
useffect(()=>{
projectFirestore.collection(“posts”).get()然后(res=>{
设refs=[];
res.forEach(数据=>{
refs.push([data.data().mylatude,data.data().myLongitude]);
});
setMapRefs(参考)
});
}, []);
返回(
{mapRefs.map(coord=>(
...
))}
)
您需要使用来呈现UI元素,并且
mapRefs
useffect
之外不可用

像这样试试

const [mapRefs, setMapRefs] = useState([])

useEffect(() => {
    projectFirestore.collection("posts").get().then(res => {
       let refs = [];
       res.forEach(data => {
          refs.push([data.data().myLatitude, data.data().myLongitude]);    
       });
       setMapRefs(refs)
    });
}, []);

return (
  <ReactMapGL>
    { mapRefs.map(coord => (
        <Marker latitude={coord[0]} longitude={coord[1]}>
            <div>
                ...
            </div>
        </Marker>
    ))}
</ReactMapGL>
)
const[mapRefs,setMapRefs]=useState([]
useffect(()=>{
projectFirestore.collection(“posts”).get()然后(res=>{
设refs=[];
res.forEach(数据=>{
refs.push([data.data().mylatude,data.data().myLongitude]);
});
setMapRefs(参考)
});
}, []);
返回(
{mapRefs.map(coord=>(
...
))}
)

“不起作用”是以什么方式出现的?你有错误吗?错误的数据?还有什么吗?嗨,伙计,对不起,我错过了。它没有输出任何东西,可能是由于“mapRefs&&”条件的原因。“以什么方式不工作?”?你有错误吗?错误的数据?还有什么吗?嗨,伙计,对不起,我错过了。它没有输出任何内容,可能是由于“mapRefs&&”条件的原因。你好,纳伦,谢谢你的帮助。我得到一个“res.map不是函数”错误。这是如何工作的呢`res.forEach(data=>{mapRefs.push([data.data().myLatitude,data.data().myLongitude]);})``改变你的逻辑,现在检查!超级棒,我的朋友,你绝对是个明星⭐ (我想我也明白了。)谢谢你,祝你今天愉快!很高兴能帮忙,祝你今天愉快!你好,纳伦,谢谢你的帮助。我得到一个“res.map不是函数”错误。这是如何工作的呢`res.forEach(data=>{mapRefs.push([data.data().myLatitude,data.data().myLongitude]);})``改变你的逻辑,现在检查!超级棒,我的朋友,你绝对是个明星⭐ (我想我也明白了。)谢谢你,祝你今天愉快!很高兴能帮忙,祝你今天愉快!