我正在尝试使用Underscore.js从Firebase返回的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”, “查尔斯达尔文”,“查尔斯达尔文”,“科比”]
的javascript:
$ scope.something = _.groupBy(userRef,'userRef.name');
输出:
{ “未定义”:[NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,{ “日期”: “2014-02-15T03:39:16.954Z”,”说明“:”非常感谢!“,”名称“:”抢“,”评分“:5,”角色“:”给予“},{”日期“:”2014-02-15T03:40:27.858Z“ ,“描述”:“这很有趣!”,“名称”:“Cobie”,“评级”:4,“角色”:“收到”},{“日期”:“2014-02-15T22:42:31.013 Z“,”“描述”:“带来了一些非常美味的饼干”,“名称”:“约翰史密斯”,“评级”:3,“角色”:“给予”},{“日期”:“2014-02- 15T22:49:08.448Z“,”“描述”:“查尔斯非常耐心。”,“名字”:“查尔斯达尔文”,“评级”:“2”,“角色”:“收到”},{“日期” :“2014-02-16T02:28:47.950Z”,“描述”:“关于物种的起源...... yaddi daddi da ....”,“名称”:“查尔斯达尔文”,“角色”:“ Gave“},{”date“:”2014-02-16T02:29:07.887Z“,”description“:”让我在没有烦恼的情况下完成计算机工作。“,”名称“:”Cobie“,”作用 “:” 都给“}]}
如何深入JSON以不返回空值?或者,如果有更好的方法可以完全从JSON中删除标题?
答案 0 :(得分:0)
首先使用下划线的_.without函数删除空字符串,解决了这个问题。
$scope.allcontacts = _.without(messy_contacts, "")
答案 1 :(得分:0)
您可以使用underscore compact。
$ scope.allcontacts = _.compact(messy_contacts);