Javascript 如何将每行元素转换为数组,以便只能选择一个元素,如数据[2]
这是我的代码,我从firebase存储中收到了数据Javascript 如何将每行元素转换为数组,以便只能选择一个元素,如数据[2],javascript,firebase,react-native,indexing,foreach,Javascript,Firebase,React Native,Indexing,Foreach,这是我的代码,我从firebase存储中收到了数据 listRef.listAll().then((res) => { res.prefixes.forEach((folderRef) => { // All the prefixes under listRef. // You may call listAll() recursively on them. }); res.items.forEach((itemRef, index) => {
listRef.listAll().then((res) => {
res.prefixes.forEach((folderRef) => {
// All the prefixes under listRef.
// You may call listAll() recursively on them.
});
res.items.forEach((itemRef, index) => {
itemRef.getDownloadURL().then((url) => {
console.log(`${index} ${url}`)
})
});
})
这是我的输出结果,如0https://myfirebaseapp.com/videos/nice.mp4
1https://myfirebaseapp.com/videos/bad.mp4
2https://myfirebaseapp.com/videos/funny.mp4
[我只想要这个元素而不是整个列表]3https://myfirebaseapp.com/videos/good.mp4
4https://myfirebaseapp.com/videos/sad.mp4
您应该在
forEach
中放入if语句,检查索引(如果您通常想要列表中的第三个元素)或视频名称(如果您特别想要该视频)您可以在数组中使用。下面是一个例子:
var项目=[
'https://myfirebaseapp.com/videos/nice.mp4',
'https://myfirebaseapp.com/videos/bad.mp4',
'https://myfirebaseapp.com/videos/funny.mp4',
'https://myfirebaseapp.com/videos/good.mp4',
'https://myfirebaseapp.com/videos/sad.mp4'
]
var funcy=items.find(x=>x.endsWith('funcy.mp4');
console.log(有趣)代码>itemRef的所有属性是什么?有没有办法知道你想要https://myfirebaseapp.com/videos/funny.mp4
无需调用itemRef.getDownloadURL()
?@TomFaltesek Yes,这些是itemRef的属性和方法-bucket、child()、fullPath、getDownloadURL()、list()、listAll()、name、parent、put、putString()、root、storage、toString()。如果给定完整路径
或名称
属性,您是否能够知道有趣的.mp4
是否是您想要的。输出为多行“字符串”。有没有办法将每一行转换成一个数组,这样我就可以选择类似的数据[2]在下面的示例中,我在调用getDownloadURL()
之前尝试选择该项。然后,我们就有希望避免在循环中调用这个等待的函数。谢谢:D