Javascript 在对象数组中循环以提取特定数据
我正在构建一个node express angular twitter状态分析器,并试图找出如何从中提取tex并将其分配给一个长字符串以供以后使用 我正在尝试提取如下用户状态:Javascript 在对象数组中循环以提取特定数据,javascript,angularjs,node.js,twitter,Javascript,Angularjs,Node.js,Twitter,我正在构建一个node express angular twitter状态分析器,并试图找出如何从中提取tex并将其分配给一个长字符串以供以后使用 我正在尝试提取如下用户状态: client.get('statuses/user_timeline', {params, count:20}, function(error, tweets, response) { var jsonObject; if (!error) { analyze.analyze(tweet
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;
我当前的代码如下所示:
[
{
"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;
功能分析(数据){
对于(变量i=0;i
目前,我在分析功能的输出中只收到一条tweet。我做错了什么
多谢各位 您发布的响应不是有效的JSON,您可以再次检查它吗?要提取包含所有twitts文本的数组,而不包含
。
,您可以执行以下操作:
function analyze(data) {
return data.map(function(item) {
return item.text.replace('#' , '');
});
}
module.exports.analyze = analyze;
您正在返回console.log() Return语句暂停函数的执行。只需删除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;
功能分析(数据){
对于(变量i=0;i
如果你想返回函数的结果,你必须将它们存储在for循环之外的一个变量中,在每次迭代时用tweet将其压缩,然后返回该值。response
JSON
无效。我知道它不是JSON。它似乎是一组物体。