I am looking to filter an array based on ids from another array. Specifically, I have an array of tweet ids associated with a certain hashtag in the object I'm working with.
h.tweet_ids = [*someTweetID*, *someOtherTweetID*, …]
On the other hand, I also have a userTimeline array that includes all tweets sent by the user who is logged in.
The structure of the userTimeline array looks like this ():
[
{
"coordinates": null,
"favorited": false,
"truncated": false,
"created_at": "Wed Aug 29 17:12:58 +0000 2012",
"id_str": "240859602684612608",
...
},
{
"coordinates": null,
"favorited": false,
"truncated": false,
"created_at": "Sat Aug 25 17:26:51 +0000 2012",
"id_str": "239413543487819778",
...
}
]
My goal is to filter the userTimeline array to only retrieve tweets whose ids match the ones in h.tweet_ids
.
So far, I've attempted using nested loops, but I've encountered issues with it sometimes going into an infinite loop...
$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
}
};
Thank you for your assistance!
Cheers ^Q