2016-12-10 1 views
1

ノードエクスプレス角ツイッターステータスアナライザーを構築していますが、後で使用するためにそれを1つの長い文字列に割り当てる方法を理解しようとしています。特定のデータを取り出すためにオブジェクトの配列をループする

私はこのようなユーザーのステータスを引くしようとしている

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; 

現在、私は私の分析機能からの私の出力に1つのみのツイートを取得します。私は間違って何をしていますか?

ありがとうございます。

+0

レスポンス 'JSON'は無効です。 –

+0

私はそれがJSONではないことを知っています。それはオブジェクトの配列です。 – Quesofat

答えて

2

。単にそのようなreturn文削除:あなたは、関数の結果を返すようにしたい場合は、forループの外で変数に格納する必要があります

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; 

を、つぶやきで各反復を、それをCONCAT、およびその値を返します。

1

あなたが投稿した回答は有効なJSONではありません。あなたはそれを再度チェックすることができますか?

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

module.exports.analyze = analyze; 
0

あなたは(にconsole.logを戻ってきている)

戻りステートメントは、関数の実行を停止:あなたが行うことができます#なく、すべてのtwittsテキストを持つ配列を引き出す

関連する問題