【问题标题】:Looping through array of objects to pull specific data out循环遍历对象数组以提取特定数据
【发布时间】:2017-04-25 23:50:48
【问题描述】:

我正在构建一个 node express angular twitter 状态分析器,我试图弄清楚如何从中提取 tex 并将其分配给一个长字符串以供以后使用。

我正在尝试像这样提取用户状态:

   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;

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

谢谢。

【问题讨论】:

  • 回复JSON无效。
  • 我知道这不是 JSON。这似乎是一个对象数组。

标签: javascript angularjs node.js twitter


【解决方案1】:

要提取一个包含所有 twitts 文本的数组,而不需要 #,您可以这样做:

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

module.exports.analyze = analyze;

【讨论】:

    【解决方案2】:

    您发布的响应不是有效的 JSON,您能仔细检查一下吗?

    【讨论】:

      【解决方案3】:

      您正在返回 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 循环之外的变量中,每次迭代将其与推文连接,然后返回该值。

      【讨论】:

        猜你喜欢
        • 2018-05-03
        • 2016-09-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-19
        • 1970-01-01
        • 2016-07-28
        • 2017-01-29
        相关资源
        最近更新 更多