我想使用Underscore.js從Firebase返回的JSON中獲取一些數據,但返回的空值很多。該JSON是從火力地堡返回目前看起來是這樣的:如何從Firebase中移除JSON數據中的額外圖層?
{
"-JFnc68gIRFohkWKBP05":
{
"date":"2014-02-15T03:39:16.954Z",
"description":"Thanks so much!",
"name":"Rob",
"role":"Give"
},
"-JFncNSO4G_hNm0YySTA":
{
"date":"2014-02-15T03:40:27.858Z",
"description":"This is fun!",
"name":"Cobie",
"role":"Received"
},
"-JFrhlpgCWxJnDETM1gg":
{
"date":"2014-02-15T22:42:31.013Z",
"description":"Brought over some really delicious cookies",
"name":"John Smith",
"role":"Gave"
},
"-JFrjHlV-fsOVHyTXHZJ":
{
"date":"2014-02-15T22:49:08.448Z",
"description":"Charles was wonderfully patient.",
"name":"Charles Darwin",
"role":"Received"
},
"-JFsWZPbL6_j-9nQwP29":
{
"date":"2014-02-16T02:28:47.950Z",
"description":"On the Origin of Species... yaddi daddi da....",
"name":"Charles Darwin",
"role":"Gave"
},
"-JFsWdH61Y-I01-rqn_n":
{
"date":"2014-02-16T02:29:07.887Z",
"description":"Let me off to do my computer work without bugging me.",
"name":"Cobie",
"role":"Gave"
}
}
當我使用Underscore.js提取數據,這些都是怎麼弄出來的樣品:
的javascript:
$scope.allNames = _.pluck($scope.data, 'name');
輸出:
[「」,「」,「」,「」,「」,「」,「」,「」,「」,「」,「」,「Rob」,「Cobie」,「John Smith」,「Charles達爾文」, 「達爾文」, 「一見鍾情。但是造化」]
的javascript:
$ scope.something = _.groupBy(userRef, 'userRef.name');
輸出:
{ 「未定義」:[NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,{ 「日期」:「2014- 02-15T03:39:16.954Z「,」description「:」非常感謝!「,」name「:」Rob「,」rating「:5,」role「:」Give「},{」date「:」 2014-02-15T03:40:27.858Z「,」description「:」這很好玩!「,」name「:」Cobie「,」rating「:4,」role「:」Received「},{」date「 :「2014-02-15T22:42:31.013Z」,「描述」:「帶來一些非常美味的餅乾」,「名稱」:「約翰史密斯」,「評價」:3,「角色」:「給予」} ,{「date」:「2014-02-15T22:49:08.448Z」,「描述」:「Charles非常耐心」,「名字」:「查爾斯達爾文」,「評價」:「2」,「角色「:」Received「},{」date「:」2014-02-16T02:28:47.950Z「,」description「:」On the Origin of種類... yaddi daddi da ....「,」name「:」Charles Darwin「,」role「:」Gave「},{」date「:」2014-02-16T02:29:07.887Z「,」說明「:」讓我過做我的電腦工作不纏着我「」名「:」一見鍾情。但是造化」,‘角色’:‘都給’}]}
我如何獲得足夠深成JSON不返回空值?或者,如果有更好的方法從JSON中完全刪除頭文件?
你使用哪個版本的'Underscore.js'? –
1.6.0。我今天剛剛從官方網站上下載了js文件。 –
我在'underscorejs.org'中用chrome控制檯測試了你的代碼,但是我的輸出是:'[「Rob」,「Cobie」,「John Smith」,「Charles Darwin」,「Charles Darwin」,「Cobie」] ' –