过滤数组以匹配其他数组

时间:2015-07-23 09:19:12

标签: javascript arrays angularjs twitter filter

我需要能够根据另一个数组中包含的id过滤数组。基本上,我有一个tweet id数组,在我操作的hashtag对象中包含一个特定的hashtag。 h.tweet_ids = [*someTweetID*, *someOtherTweetID*, …]

另一方面,我有userTimeline数组,其中包含验证用户发送的所有推文。

它的结构如此(https://dev.twitter.com/rest/reference/get/statuses/user_timeline):

[
  {
    "coordinates": null,
    "favorited": false,
    "truncated": false,
    "created_at": "Wed Aug 29 17:12:58 +0000 2012",
    "id_str": "240859602684612608",
    "entities": {
      …
    },
    "in_reply_to_user_id_str": null,
    "contributors": null,
    "text": "Introducing the Twitter Certified Products Program: https://t.co/MjJ8xAnT",
    "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": {
      …
    },
    "in_reply_to_screen_name": null,
    "source": "<a href="//sites.google.com/site/yorufukurou/\"" rel="\"nofollow\"">YoruFukurou</a>",
    "in_reply_to_status_id": null
  },
  {
    "coordinates": null,
    "favorited": false,
    "truncated": false,
    "created_at": "Sat Aug 25 17:26:51 +0000 2012",
    "id_str": "239413543487819778",
    "entities": {
      …
    },
    "in_reply_to_user_id_str": null,
    "contributors": null,
    "text": "We are working to resolve issues with application management & logging in to the dev portal: https://t.co/p5bOzH0k ^TS",
    "retweet_count": 105,
    "in_reply_to_status_id_str": null,
    "id": 239413543487819778,
    "geo": null,
    "retweeted": false,
    "possibly_sensitive": false,
    "in_reply_to_user_id": null,
    "place": null,
    "user": {
      …
    },
    "in_reply_to_screen_name": null,
    "source": "<a href="//sites.google.com/site/yorufukurou/\"" rel="\"nofollow\"">YoruFukurou</a>",
    "in_reply_to_status_id": null
  }
]

我想要实现的是过滤userTimeline以仅检索其id(即userTimleine[i].id_str)是h.tweet_ids中的ID的推文。

目前,我在循环中尝试了一个循环,但不知何故它有时会失败进入一个无限循环......

$scope.showTweetsForHashtag = function(h){
  var userTimeline = $scope.userTimeline,
     tweetId=h.tweetId,
     hTweets = [];
  for (var i = 0; i < tweetId.length; i++) {
     for (var j = 0; j < userTimeline.length; j++) {
        if (userTimeline[j].id_str===tweetId[i]) {
           hTweets.push(userTimeline[j]);
        }
     }
  }
  $scope.selHash=h.Hashtag;
  $scope.hTweets=hTweets;
  if (!$scope.hModal) {
     $scope.hModal=true
  }
};

提前感谢您的帮助!

干杯 ^ Q

3 个答案:

答案 0 :(得分:0)

使用lodash,我会做类似的事情:

&#13;
&#13;
var users = [
  {
    "coordinates": null,
    "favorited": false,
    "truncated": false,
    "created_at": "Wed Aug 29 17:12:58 +0000 2012",
    "id_str": "240859602684612608",
    "entities": {
    },
    "in_reply_to_user_id_str": null,
    "contributors": null,
    "text": "Introducing the Twitter Certified Products Program: https://t.co/MjJ8xAnT",
    "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": {
    },
    "in_reply_to_screen_name": null,
    "source": "",
    "in_reply_to_status_id": null
  },
  {
    "coordinates": null,
    "favorited": false,
    "truncated": false,
    "created_at": "Sat Aug 25 17:26:51 +0000 2012",
    "id_str": "239413543487819778",
    "entities": {
    },
    "in_reply_to_user_id_str": null,
    "contributors": null,
    "text": "We are working to resolve issues with application management & logging in to the dev portal: https://t.co/p5bOzH0k ^TS",
    "retweet_count": 105,
    "in_reply_to_status_id_str": null,
    "id": 239413543487819778,
    "geo": null,
    "retweeted": false,
    "possibly_sensitive": false,
    "in_reply_to_user_id": null,
    "place": null,
    "user": {
    },
    "in_reply_to_screen_name": null,
    "source": "",
    "in_reply_to_status_id": null
  }
];

var tweet_ids = ["240859602684612608", "000000"];
var matchIds = _.intersection(_.pluck(users, 'id_str'),tweet_ids );
var matchTweets = _.filter(users,function(userTmp){
  return matchIds.indexOf(userTmp.id_str) !== -1 ;
})

console.log(matchTweets);

$("body").append(JSON.stringify(matchTweets))
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.0/lodash.min.js"></script>

<body></body>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

嗯,没有Lodash,我找到了一条路!

$scope.showTweetsForHashtag = function(h) {
    var userTimeline = $scope.userTimeline,
        tweetId = h.tweetId,
        hTweets = [];
    if (Array.isArray(tweetId)) {
        for (var i = 0; i < tweetId.length; i++) {
            var hTweet = userTimeline.filter(function(el) {
                return el.id_str === tweetId[i];
            });
            hTweets = hTweets.concat(hTweet);
        }
        $scope.hTweets = hTweets;
    } else {
        $scope.hTweets = userTimeline.filter(function(el) {
            return el.id_str === tweetId;
        });
    }
    $scope.selHash = h.Hashtag;
    if (!$scope.hModal) {
        $scope.hModal = true
    }
};

非常感谢您的帮助!

^ Q

答案 2 :(得分:0)

使用Array.prototype.filter之类的var tweet_ids = [1,2,3,5,8,13,21]; var tweets = [{ 'id_str': 1 }, { 'id_str': 4 }, { 'id_str': 8 }, { 'id_str': 11 }]; var filtered = tweets.filter(function (t) { for (var i = 0, len = tweet_ids.length; i < len; i++) { if (tweet_ids[i] == t.id_str) return true; } }); console.log(filtered);

var polyFeatures = featureOverlay.getSource();

var coordsMulti = [];
var coordsSingle = [];

polyFeatures.forEachFeature(function (polyFeature) {
    if (polyFeature.getGeometry().getType() === 'Polygon') {
        // this will get you all polygon coordinates
        coordsMulti.push(polyFeature.getGeometry().getCoordinates());

        // this will get you central coordinate of polygon
        coordsSingle.push(polyFeature.getGeometry().getInteriorPoint());
    }
});
相关问题