通过对象数组循环来提取特定的数据

我正在build立一个节点快速叽叽喳喳状态分析器,我想弄清楚如何拉出tex,并将其分配给一个长string供以后使用。

我试图拉这样的用户状态:

client.get('statuses/user_timeline', {params, count:20}, function(error, tweets, response) { var jsonObject; if (!error) { analyze.analyze(tweets); } }); 

响应看起来像这样:

  [ { "coordinates": null, "favorited": false, "truncated": false, "created_at": "Wed Aug 29 17:12:58 +0000 2012", "id_str": "240859602684612608", "entities": { "urls": [ { "expanded_url": "/blog/twitter-certified-products", "url": "", "indices": [ 52, 73 ], "display_url": " } ], "hashtags": [ ], "user_mentions": [ ] }, "in_reply_to_user_id_str": null, "contributors": null, "text": "Introducing the Twitter Certified Products Program: ", "retweet_count": 121, "in_reply_to_status_id_str": null, "id": 240859602684612608, "geo": null, "retweeted": false, "possibly_sensitive": false, "in_reply_to_user_id": null, "place": null, "user": { "profile_sidebar_fill_color": "DDEEF6", "profile_sidebar_border_color": "C0DEED", "profile_background_tile": false, "name": "Twitter API", "profile_image_url": ", "created_at": "Wed May 23 06:01:13 +0000 2007", "location": "San Francisco, CA", "follow_request_sent": false, "profile_link_color": "0084B4", "is_translator": false, "id_str": "6253282", "entities": { "url": { "urls": [ { "expanded_url": null, "url": "", "indices": [ 0, 22 ] } ] }, "description": { "urls": [ ] } }, 

而我目前的代码如下所示:

 function analyze(data) { for (var i = 0; i < data.length; i++) { tweet = data[i]['text']; tweet = tweet.replace('#' , ''); return console.log(tweet); } } module.exports.analyze = analyze; 

目前,在我的分析function中,我的输出中只有一条推文。 我究竟做错了什么?

谢谢。

要用#所有的twitts文本抽出一个数组,你可以这样做:

 function analyze(data) { return data.map(function(item) { return item.text.replace('#' , ''); }); } module.exports.analyze = analyze; 

你发布的回复不是有效的JSON,你可以仔细检查一下吗?

你正在返回console.log()

返回语句停止函数的执行。 简单地删除这样的return语句:

 function analyze(data) { for (var i = 0; i < data.length; i++) { tweet = data[i]['text']; tweet = tweet.replace('#' , ''); console.log(tweet); } } module.exports.analyze = analyze; 

如果要返回函数的结果,则必须将它们存储在for循环外部的variables中,每次迭代都会将其与tweet进行连接,然后返回该值。