1

I have an Array object that looks like this:

var wordList = new Array();
$.get('words.txt', function(data){
    wordList = data.split('\n');
});

When I do this:

return jQuery.inArray(word, wordList)!==-1;

Using a word that I //know// is in the array, it'll still return false. It might be that the array is too large (it contains 173,139 strings) - is this it? In this case, should I give up on using an array, or is there a different way to go about searching the array?

I checked the type of the word and the elements in the array - both are string according to JavaScript's typeof.

What's odd is that I've used inArray with the same words and a different array, and it works just fine - the difference is the way the arrays are generated (one is through the same input I get word from, and one is by splitting lines in a .txt file).

Also, when I try to call the element explicitly by its index, it shows up, so I'm sure the array is loading in. I'm using high index numbers too; calling wordList[173138] doesn't make the response time slow in any way.

Why isn't inArray() working here?

Arnav
  • 115
  • 2
  • 11
  • You can't use teh value of `wordList` outside of the `$.get()` callback because of the asynchronous nature of ajax.... what ever you want to do with `wordList` has to be done in the success handler – Arun P Johny Mar 22 '14 at 05:57
  • Oh, sorry, I tried looking for a duplicate question before I posted this - that's embarrassing. – Arnav Mar 22 '14 at 05:58
  • Wait... I'm using `wordList[173178]` outside the `$.get()` callback. I can use `wordList[index]` but can't use `inArray()` on wordList? Or am I missing something again? – Arnav Mar 22 '14 at 06:10
  • Can you show us your `wordList data`? – Rohan Kumar Mar 22 '14 at 06:17
  • Sure! I'm using the ENABLE word list, found here: https://code.google.com/p/dotnetperls-controls/downloads/detail?name=enable1.txt – Arnav Mar 22 '14 at 06:21
  • You tried it like, `var wordsList=['abc','test',1];alert('test index='+$.inArray('test',wordsList)+'\n'+'1 index='+$.inArray(1,wordsList)+'\n'+'x index='+$.inArray('x',wordsList));` [Demo](http://jsfiddle.net/rohankumar1524/BLrsC/) – Rohan Kumar Mar 22 '14 at 06:24
  • That's an example of inArray working properly, but when I use it on my array, it returns -1 even if I give it a word that I checked the list for. For example, if I did `inArray('biology', wordList)!==-1`, it would return false. – Arnav Mar 22 '14 at 06:27
  • Its working, you may have some other problem, I used some words from your text file and it works for me, see [another demo](http://jsfiddle.net/rohankumar1524/BLrsC/1/) – Rohan Kumar Mar 22 '14 at 06:40
  • For future Googlers: after much exasperation, I ended up fixing the problem by doing `data.split('\r\n')` instead of `data.split('\n')`. It's some different kind of newline or something, relating to Windows. – Arnav Mar 22 '14 at 07:22
  • @Aranv, add it as an answer to the question. That way it will get the full attention it needs. After a time delay, you can even mark it as the answer to make it really clear :) – Matt Tester Mar 22 '14 at 08:11
  • will do. Thank you for helping me help others. – Arnav Mar 22 '14 at 16:52

1 Answers1

0

I put this in the comments originally, but was told to make it an answer - this should make it easier to find. For anyone having trouble looping through arrays like this, check the way you're splitting your array if it's split by newlines - my array was causing some trouble because I was using data.split('\n') where I should've been using data.split('\r\n'). After fixing that, everything was fine.

Arnav
  • 115
  • 2
  • 11