0

I want to check if news_text variable contains the same string inside the text variable while looping. The string that news _text could have is :

"Yemen minister says Saleh trying to spoil transition http://t.co/aXOQPMzf"

and the string for the text variable is this

"RT @Reuters: Yemen minister says Saleh trying to spoil transition http://t.co/aXOQPMzf"

      for (var i = 0; i < full_array.length; i++) {
          var user = full_array[i].user;
          var date = full_array[i].date;
          var profile_img = full_array[i].profile_img;
          var text = full_array[i].text;
          var news_user = full_array[i].news_user;
          var news_date = full_array[i].news_date;
          var news_profile_img = full_array[i].news_profile_img;
          var news_text = full_array[i].news_text;

          if(text==news_text){
              geocode (user,date,profile_img,text,news_user,news_date,news_profile_img,news_text);
          }
    }

anyone that could help?

anjel
  • 1,355
  • 4
  • 23
  • 35
  • 1
    please search the Web/SO before asking: exact duplicate of [JavaScript: string contains](http://stackoverflow.com/questions/1789945/javascript-string-contains) :-) – Bergi Sep 23 '12 at 09:34
  • I disagree - it needs something stronger that 'string contains'. It really needs a fuzzy striung match algorith. See my answer below. – Peter Smith Sep 23 '12 at 09:40
  • Sorry i havent explained correctly. i want to get the first 10 characters from variable news_text and check if its inside the text variable – anjel Sep 23 '12 at 09:42
  • @PeterSmith: I don't think the OP asked for a fuzzy search - he might want a search that also finds substring matches, but nothing fuzzy and his example is a whole-string-search. – Bergi Sep 23 '12 at 09:43

2 Answers2

0

Given the complexity of fuzzy string matches I suggest that you look at sites like this from Google - Yeti Witch. Google search for Soundex and JavaScript has other examples.

Good luck.

Peter Smith
  • 5,528
  • 8
  • 51
  • 77
0

just use

if (text.indexOf(news_text) > -1) // text contains news_text
    geocode(…);

Edit: if you only need to search for the first 10 chars of news_text, use

if (text.indexOf(news_text.substr(0, 10)) > -1) // text contains news_text
    geocode(…);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • yeah but why does it give me text is undefined? – anjel Sep 23 '12 at 09:48
  • maybe because `text` *is* undefined? Your `text` variable needs to be a string (which was not enforced by your current code) – Bergi Sep 23 '12 at 10:02
  • I run typeof over all text variables and it said undefined though all text variables have string inside of them. – anjel Sep 23 '12 at 10:07
  • No, obviously they don't. If your `fullarray` is the `newsarray` concatenated with the `usertweets` from [this question](http://stackoverflow.com/questions/12446776/how-to-request-twitter-api-without-entering-a-recursion), some of the objects have a `text` property but no `news_text`, the others have a `news_text` property but no `text`. – Bergi Sep 23 '12 at 10:13
  • so its better not to concatenate the two arrays into one and do the checking between the two?? – anjel Sep 23 '12 at 10:20
  • It seems like. Please, describe what you *want* not how you think it should work when asking any questions… – Bergi Sep 23 '12 at 10:26
  • Can you check this fiddle and tell me why is not returning the user_tweets ?? http://jsfiddle.net/SLEd9/1/ – anjel Sep 23 '12 at 11:16
  • Which two arrays? What is your goal in comparing them? – Bergi Sep 23 '12 at 11:16
  • @fiddle: you can only return one value in a `return` statement, you are actually using the [comma operator](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Comma_Operator) in here – Bergi Sep 23 '12 at 11:20
  • the two arrays are user_tweets and news_array. i want to place each news tweet(from news arrays) into a div along with users who posted for the same news on that news tweet. – anjel Sep 23 '12 at 11:24
  • So, actually you don't need to compare the whole `usertweets` with a news message, but only the result of the current search? – Bergi Sep 23 '12 at 11:29
  • no i want to compare all the objects from user tweets so that i show all the posts related to that news tweet from the array news_array – anjel Sep 23 '12 at 11:37
  • so how can i return both arrays?? – anjel Sep 23 '12 at 13:29
  • pack them into an object or array? – Bergi Sep 23 '12 at 14:12
  • No idea where you got that `array=newstweets()` from - it's a callback! http://jsfiddle.net/SLEd9/8/ – Bergi Sep 23 '12 at 14:50
  • can you tell me what am doing wrong here?Am trying to get elements of the two arrays with the same urls. http://jsfiddle.net/SLEd9/10/ – anjel Sep 23 '12 at 15:35
  • ok can you check this one i updated http://jsfiddle.net/SLEd9/12/ .thanks @Bergi !!!! – anjel Sep 23 '12 at 18:11