【发布时间】:2014-02-16 03:29:31
【问题描述】:
我正在尝试使用 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,{"date":"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.013 Z","description":"带来了一些非常美味的饼干","name":"John Smith","rating":3,"role":"Gave"},{"date":"2014-02- 15T22:49:08.448Z","description":"Charles 非常有耐心。","name":"Charles Darwin","rating":"2","role":"Received"},{"date" :"2014-02-16T02:28:47.950Z","description":"关于物种起源... yaddi daddi da....","name":"Charles Darwin","role":" Gave"},{"date":"2014-02-16T02:29:07.887Z","description":"让我去做我的电脑工作,不要打扰我。","name":"Cobie","角色":"给予"}]}
如何深入了解 JSON 而不返回空值?或者,是否有更好的方法从 JSON 中完全删除标头?
【问题讨论】:
-
您使用哪个版本的
Underscore.js? -
1.6.0.今天刚从官网抓了个js文件。
-
我用 chrome 控制台在
underscorejs.org中测试了你的代码,但我的输出是:["Rob","Cobie","John Smith","Charles Darwin","Charles Darwin","Cobie"] -
抱歉,在显示的所有内容的顶部有一个附加层。更像是这样(安卓手机不让我退货):SOMETHING { "-JFnc68gIRFohkWKBP05": { "date":"2014-02-15T03:39:16.954Z", "description":"非常感谢!", "name":"Rob", "role":"Give" }, "-JFncNSO4G_hNm0YySTA": { "date":"2014-02-15T03:40:27.858Z", "description":"这是有趣!”、“名称”:“Cobie”、“角色”:“收到”}、“-JFrhlpgCWxJnDETM1gg”:{“日期”:“2014-02-15T22:42:31.013Z”、“描述”:“带来在一些非常美味的饼干上”,“name”:“John Smith”,“role”:“Gave”},...
-
回到电脑上。看起来是这样的:
标签: javascript json underscore.js firebase