【发布时间】:2018-10-04 20:51:06
【问题描述】:
我在按时间值对 React-native 中的对象列表进行排序时遇到问题。我在网上找不到答案。你有什么建议吗?
我的控制台登录原始对象的 chrome:
Firebase 数据库结构
JSON.stringify(myObject) 输出:
myObject={"-LAqmXKSdVVH6wirFa-g":
{"desc":"fdf","price":"rrrr","receiveHelp":true,"subject":"Single Variable
Calculus","time":1524558865757,"title":"Test1"},"-LAqmZlBFGoygfTsGQ0e":
{"desc":"dsfdsfd3","price":"333","receiveHelp":true,"subject":"Single
Variable Calculus","time":1524558875724,"title":"Test2"},"-
LAqmcipUjlwLWTrRCw4":
{"desc":"werwerwe55","price":"44","receiveHelp":true,"subject":"Single
Variable Calculus","time":1524558891956,"title":"Test3"},"-
LArMeYfHG6QMg_Frn9A":
{"desc":"3","price":"3","receiveHelp":true,"subject":"Single Variable
Calculus","time":1524568598762,"title":"Annons 1"},"-LArMjg5MkF5cMPZd_Fz":
{"desc":"2222","price":"2","receiveHelp":true,"subject":"Single Variable
Calculus","time":1524568619782,"title":"Annons2"},"-LArNM-3Ij60XmSOBwvr":
{"desc":"22","price":"","receiveHelp":true,"subject":"Single Variable
Calculus","time":1524568780803,"title":"Hej1"},"-LArNPugIfX1pPVZJ11e":
{"desc":"f","price":"2","receiveHelp":true,"subject":"Single Variable
Calculus","time":1524568796844,"title":"Hej2f"}}
到目前为止我所尝试的:
firebase.database().ref('/users').once('value').then((snapshot) => {
const ads = snapshot.val();
let myObject = {};
Object.keys(ads).map((objectKey) => {
const value = ads[objectKey];
myObject = Object.assign(value.ads, myObject);
});
//Here i want to sort myObject by time and get the latest first
console.log(myObject)
【问题讨论】:
-
你应该在这篇文章中包含你的对象结构和数据,而不是作为外部链接。
-
您的数据有多个级别,那么您要在哪个级别排序。你的问题解释得不好
-
我已经打印了 JSON.stringify(myObject) 输出,所以这是我想要排序的输出
-
您将在每次迭代中覆盖
myObject值,因此您将如何获得数组或集合。 -
我应该怎么做而不是覆盖 myObject 来收集所有用户的广告?然后排序?
标签: javascript firebase react-native firebase-realtime-database jsx