如何从Firebase检索日期并将其存储到数组中?

时间:2019-09-15 17:58:05

标签: javascript html arrays sorting firebase-realtime-database

我想按升序和降序对日期列进行排序。在此之前,我需要将Firebase中的日期存储到and数组中,而我坚持使用该部分。请注意,我对此很陌生。

enter image description here

这是我用来将Firebase中的日期显示到表格列中的代码。

   rootRef.on('child_added', function(childSnapshot) {
        dates = [];
        rootRef = childSnapshot.val();
        tr.appendChild(createCell(rootRef.date));
        tr.appendChild(createCell(rootRef.time));
        table.appendChild(tr);
   });


  function createCell(text) {
        var td = document.createElement('td');
        td.appendChild(document.createTextNode(text));
        return td;
   }

获取日期数组后,我打算使用此代码比较日期。

let sortedDates = dates.sort((a, b) => { //compare function
    const monthArray = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; //month index

 //split a and b
    a = a.split('-'); 
    b = b.split('-');

0 个答案:

没有答案