Javascript 如何映射此数据以达到数据属性?
我正试图清理从firebase收到的数据,以便在平面列表中查看它们。如何将数据清理到一个简单的数组中,以便在FlatList中进行迭代 编辑!在我的数据库中还有很多其他的硬币,我想把它们放入扁平列表中。因此,我正在寻找的解决方案是查看我的平面列表中的所有这些硬币,然后显示它们的数据,如价格、市值等 我的数据当前存储在一个状态中,如下所示Javascript 如何映射此数据以达到数据属性?,javascript,reactjs,firebase,react-native,firebase-realtime-database,Javascript,Reactjs,Firebase,React Native,Firebase Realtime Database,我正试图清理从firebase收到的数据,以便在平面列表中查看它们。如何将数据清理到一个简单的数组中,以便在FlatList中进行迭代 编辑!在我的数据库中还有很多其他的硬币,我想把它们放入扁平列表中。因此,我正在寻找的解决方案是查看我的平面列表中的所有这些硬币,然后显示它们的数据,如价格、市值等 我的数据当前存储在一个状态中,如下所示 favoriteList data is: Object { "bitcoin": Object { "-MahI
favoriteList data is:
Object {
"bitcoin": Object {
"-MahI1hCDr0CJ_1T_umy": Object {
"data": Object {
"ath": 54205,
"ath_change_percentage": -40.72194,
"ath_date": "2021-04-14T11:54:46.763Z",
"atl": 51.3,
"atl_change_percentage": 62536.71794,
"atl_date": "2013-07-05T00:00:00.000Z",
"circulating_supply": 18719656,
"current_price": 32164,
"fully_diluted_valuation": 674764316483,
"high_24h": 33004,
"id": "bitcoin",
"image": "https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1547033579",
"last_updated": "2021-05-27T10:07:02.525Z",
"low_24h": 30652,
"market_cap": 601493137412,
"market_cap_change_24h": -15118857257.119507,
"market_cap_change_percentage_24h": -2.45192,
"market_cap_rank": 1,
"max_supply": 21000000,
"name": "Bitcoin",
"price_change_24h": -641.85835686,
"price_change_percentage_1h_in_currency": 0.25769270475453127,
"price_change_percentage_24h": -1.95655,
"price_change_percentage_24h_in_currency": -1.9565521832416402,
"price_change_percentage_7d_in_currency": 4.978932125496787,
"symbol": "btc",
"total_supply": 21000000,
"total_volume": 36947814578,
},
},
},
}
firebase结构如下所示,上面的数据取自:
Object.keys(favorite.bitcoin)[idx]
这一行将索引0
中的键名输入Objectfavorite.bitcoin
因此变量key
将是您的firebase密钥
let favorite={
比特币:{
“-MahI1hCDr0CJ___________;y”:{
数据:{
电话:54205,
变动百分比:-40.72194,
日期:“2021-04-14T11:54:46.763Z”,
atl:51.3,
atl变化百分比:62536.71794,
atl_日期:“2013-07-05T00:00:00.000Z”,
循环水供应:18719656,
现价:32164,
完全稀释估值:674764316483,
高_24小时:33004,
id:“比特币”,
图片:
"https://assets.coingecko.com/coins/images/1/large/bitcoin.png?1547033579",
最后更新:“2021-05-27T10:07:02.525Z”,
低_24小时:30652,
市值:601493137412,
24小时市场变化:-15118857257.119507,
24小时内的市值变化百分比:-2.45192,
市值排名:1,
最大供应量:2100万,
名称:“比特币”,
24小时价格变化:-641.85835686,
以货币表示的价格变动百分比:0.25769270475453127,
24小时价格变化率:-1.95655,
以货币表示的24小时价格变动百分比:-1.9565521832416402,
以货币表示的价格变动百分比:4.978932125496787,
符号:“btc”,
总供应量:2100万,
总体积:36947814578,
},
},
},
};
设idx=0//索引0处的键
让key=Object.keys(favorite.bitcoin)[idx];
console.log(键)
让data=favorite.bitcoin[key].data;
console.log(data)
要从数据库中获取数据,需要查询其父引用。这将允许您执行诸如“查找当前价格超过30000美元的/favorites/bitcoin
下的所有条目”之类的操作
因为您只想查询问题中/favorites/bitcoin
下的所有数据,所以您可以执行以下操作:
/favorites/bitcoin
/favorites/bitcoin
平面列表
function getDataForFlatlistUnder(databasePath) {
return firebase.database()
.ref(databasePath)
// consider using .limitToFirst(10) or similar queries
.once("value")
.then((listSnapshot) => {
// listSnapshot contains all the data under `${databasePath}`
const arrayOfDataObjects = [];
// For each entry under `listSnapshot`, pull its data into the array
// Note: this is a DataSnapshot#forEach() NOT Array#forEach()
listSnapshot.forEach((entrySnapshot) => {
// entrySnapshot contains all the data under `${databasePath}/${entrySnapshot.key}`
const data = entrySnapshot.child("data").val();
// data is your data object
// i.e. { ath, ath_change_percentage, ath_date, atl, ... }
// add the key into the data for use with the FlatList
data._key = entrySnapshot.key;
arrayOfDataObjects.push(data);
});
return arrayOfDataObjects;
});
}
您可以在组件中使用,如下所示:
function renderItem((dataObject) => {
// TODO: render data in dataObject
});
function MyComponent() {
const [listData, setListData] = useState();
const [listDataError, setListDataError] = useState(null);
const [listDataLoading, setListDataLoading] = useState(true);
useEffect(() => {
const disposed = false;
getDataForFlatlistUnder("favorites/bitcoin")
.then((arrayOfDataObjects) => {
if (disposed) return; // component was removed already, do nothing
setListData(arrayOfDataObjects);
setListDataLoading(false);
})
.catch((err) => {
if (disposed) return; // component was removed already, do nothing
// optionally empty data: setListData([]);
setListDataError(err);
setListDataLoading(false);
});
// return a cleanup function to prevent the callbacks above
// trying to update the state of a dead component
return () => disposed = true;
}, []); // <-- run this code once when component is mounted and dispose when unmounted
if (listDataLoading)
return null; // or show loading spinner/throbber/etc
if (listDataError !== null) {
return (
<Text>
{"Error: " + listDataError.message}
</Text>
);
}
return (
<FlatList
data={listData}
renderItem={renderItem}
keyExtractor={item => item._key} // use the key we inserted earlier
/>
);
}
函数renderItem((数据对象)=>{
//TODO:在dataObject中渲染数据
});
函数MyComponent(){
const[listData,setListData]=useState();
const[listDataError,setListDataError]=useState(null);
const[listDataLoading,setListDataLoading]=useState(true);
useffect(()=>{
const=false;
getDataForFlatlistUnder(“收藏夹/比特币”)
.然后((arrayOfDataObjects)=>{
如果(disposed)return;//组件已被删除,则不执行任何操作
setListData(arrayOfDataObjects);
setListDataLoading(假);
})
.catch((错误)=>{
如果(disposed)return;//组件已被删除,则不执行任何操作
//可选空数据:setListData([]);
setListDataError(错误);
setListDataLoading(假);
});
//返回一个清理函数以防止上面的回调
//尝试更新失效组件的状态
return()=>disposed=true;
}, []); //
);
}
注意:此代码用于一次性抓取数据,如果您需要实时更新,您可以将其修改为使用。确保在
useffect
调用的unsubscribe函数中使用,以正确地清理它。了解程序员如何解释问题很有趣。或者初学者如何无法清楚地表达他们想要实现的目标。答案如下:
const formatData = (data) => {
let arr = [];
let test = Object.values(data).forEach((o) => {
Object.values(o).forEach((a) =>
Object.values(a).forEach((b) => arr.push(b))
);
setFormattedData(arr);
});
谢谢你的回答!我的列表很大,无法为每次迭代添加firebase密钥。对不起,我应该更清楚地回答这个问题。谢谢你的建议。但这并不奏效。我得到“undefined不是一个对象(评估'object.keys(favorites.bitcoin')”。无论如何,这个解决方案不适合我,因为还有许多其他硬币我想迭代,我不能将每个硬币的名字添加到代码中。这有意义吗?您也可以像上面的解决方案一样使用coin name key的动态。让我看看你现在已经尝试了什么。