javascript - Filter array to match other array -
i need able filter array based on ids contained in array. basically, have array of tweet ids contain specific hashtag within hashtag object manipulate. h.tweet_ids = [*sometweetid*, *someothertweetid*, …]
on other hand, have usertimeline array contains tweets sent authenticating user.
its structure looks (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 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 working resolve issues application management & logging in 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 } ]
what achieve filter usertimeline retrieve tweets ids (ie. usertimleine[i].id_str
) ones in h.tweet_ids
.
for moment, tried loop inside loop, somehow fails goes infinite loop...
$scope.showtweetsforhashtag = function(h){ var usertimeline = $scope.usertimeline, tweetid=h.tweetid, htweets = []; (var = 0; < tweetid.length; i++) { (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 } };
thanks in advance !
cheers ^q
with lodash :
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 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 working resolve issues application management & logging in 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))
<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>
Comments
Post a Comment