Node.js 的 30 天學習日記--第十一天

Node.js 學習第十一天

第十一天有看到幾家公司的求職條件要求都好高,對於我這個新鮮人來講實在是很難有優勢,只好繼續學多增加作品吧。

如果在看的過程中有任何錯誤歡迎寫 E-MAIL: pp840405@gmail.com 提醒我錯誤。


forEach、orderByChild - 資料排序篇

這資料排序的語法是 firebase 的,所以今天學的事如何把 firebase 資料庫裡面的資料由小到大。
orderByChild 的語法是選取要排序的子鍵,單獨使用是沒辦法排序要跟 forEach 使用來完成排序。
先看 firebase 的排序規則
基本上規則是:

  1. 子鍵值 null
  2. 子鍵值 false
  3. 子鍵值 true
  4. 子鍵值是數值由小到大
  5. 子鍵值是字串由 A 到 Z
  6. 物件定義
    以上規則都是由上到下排列
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// 排序weight的資料由小到大
//宣告物件
var people = {
"mike": {
"height" : 4,
"length" : 12.5,
"weight": 5000
},
"casper": {
"height" : 3,
"length" : 9,
"weight" : 2500
},
"bob": {
"height": "dalse",
"length" : false,
"weight" : 2500
},
"john": {
"height" : true,
"length" : 9,
"weight" : 2500
}
,
"josh": {
"height" : false,
"length" : 9,
"weight" : 2500
},
'boss':{
"length": 3
},
'frank':{
height:{'aaa':1}
}
};
//選取並新增people目錄並新增people物件資料
firebase.database().ref('people').set(people);
//宣告變數指定people子目錄
var peopleRef = firebase.database().ref('people');
// 路徑>>排序('屬性')>>讀取> forEach 依序撈出資料
peopleRef.orderByChild('height').once('value',function(snapshot){
snapshot.forEach(function(item){
console.log(item.val());
})
})

結語

這次排序規則對於要處理或顯示資料做排列的網站很有用,有需要的話可以去查詢 JavaScript 的排序如何做使用。
有任何問題歡迎寫 MAIL: pp840405@gmail.com 一起共同討論。