3

I have added the key value pairs to the dictionary as follows

    var TextFile = {};
     jQuery.get('SpellCheck.txt', function (data) {
            var Values = data.split('\n');
            for (var i = 0; i < 50; i++) {
                var val = Values[i];
                TextFile[val] = 'true';
            }
        });

Textfile has the following values

enter image description here

But the Textfile['ABCD'] is returned as undefined

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
Vaishali
  • 117
  • 1
  • 2
  • 9
  • 1
    where did you use `Textfile['ABCD']` ? inside the success callBack? or outside of it? Jquery.get is asynchronous. – Rajaprabhu Aravindasamy Mar 12 '16 at 04:55
  • 1
    If i'm not mistaken, Values[0] is the whole line (or string)- "ABCD": "true" – Jordan Mar 12 '16 at 04:56
  • Apart from what @RajaprabhuAravindasamy mentioned, jquery.get is also an HTTP Get call. I am assuming you are not using it to fetch a file from local system. I might be grossly wrong, but would like to clear that. Thanks. – Sid Mar 12 '16 at 04:57
  • @Jordan Based on her output, we could say that `data` will be like `"ABCD\nABG\nABI...."` She is splitting it and creating a new object. – Rajaprabhu Aravindasamy Mar 12 '16 at 04:58
  • I have mentioned jquery.get outside. I am reading the data from a text file where each word is in each line. So I am splitting it and adding – Vaishali Mar 12 '16 at 05:08
  • TextFile["ABCD" : "true"] is also not returning correct output. Suggest me a way – Vaishali Mar 12 '16 at 05:08
  • @Vaishali See my updated answer. – Scott Marcus Mar 12 '16 at 06:10
  • @RajaprabhuAravindasamy You marked this question as already having an answer incorrectly. The issue has nothing to do with asynchronous calls as the attempt at getting the response is correct in the success callback. There is a response coming back from the call and the OP is accessing it in the right place. The question is about accessing the response (not where, but how) correctly. The problem and solution are described in my answer below and have to do with the fact that the request is for a text file, as opposed to a JSON object. Please un-mark this question as already having an answer. – Scott Marcus Mar 12 '16 at 06:16
  • @RajaprabhuAravindasamy As a follow up, if you read the question, you can see that the OP is getting the result of the AJAX call (he posts a screen shot of those results), so he's not asking why the AJAX call didn't return a value, he's asking why accessing a part of the result isn't working for him. – Scott Marcus Mar 12 '16 at 14:26

1 Answers1

0

SOLUTION:

The issue stems from the fact that you are accessing text content via your AJAX request and not a JSON object. The words that ultimately become your property names are being turned into Strings (unlike JSON, where they would be property identifiers). Since you are working with String names, you must use Object.keys() to access those property names.

The following code demonstrates the differences between an object with properties that are identifiers and your object, which is more of a dictionary that has String names and how to access the dictionary vs. the traditional object.

SpellCheck.txt:

ABCD
ABG
ABI
ABO
AC
ACE
ACEI
ACER
ACL
ADAM
ADHD
ADL
ADPKD
ADS
AENNS
AFB
AFI
AFO
AFP

Your code:

var TextFile = {};
jQuery.get('SpellCheck.txt', function (data) {
  var values = data.split('\n');
  for (var i = 0; i < values.length; i++) {
    var val = values[i];
    TextFile[val] = 'true';
  }
});

My Tests (Placed INSIDE the AJAX success callback above):

  // Test of traditional JS object with properties that are not strings:
  var myObj = { prop1: 1, prop2: 2 }
  console.log(myObj);

  // Test of your dictionary object and how you must use Object.keys()
  // to access dictionary elements:
  console.log(TextFile);
  console.log("Name of first key in dictionary is: " + Object.keys(TextFile)[0]);
  console.log("Value of first key in dictionary is: " + TextFile[Object.keys(TextFile)[0]]);

You can see here in the console the difference in how the JS engine sees the two objects:

enter image description here

Notice how your object property names are quoted, but the myObj names aren't?

To get the result you were hoping for, call for a JSON object:


SpellCheck2.txt:

{
    "ABCD" : "",
    "ABG" : "",
    "ABI" : "",
    "ABO" : "",
    "AC" : "",
    "ACE" : "",
    "ACEI" : "",
    "ACER" : "",
    "ACL" : "",
    "ADAM" : "",
    "ADHD" : "",
    "ADL" : "",
    "ADPKD" : "",
    "ADS" : "",
    "AENNS" : "",
    "AFB" : "",
    "AFI" : "",
    "AFO" : "",
    "AFP" : "" 
}

index.html:

var result = "";
jQuery.get('SpellCheck2.txt', function (data) {
  result = JSON.parse(data);
  for (var prop in result) {
    result[prop] = 'true';
  }

  console.log("Value of result[\"ABCD\"] is: " + result["ABCD"]);

});

Result: Value of result["ABCD"] is: true

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • My text file looks like this ABCD ABG ABI ABO AC ACE ACEI ACER ACL ADAM ADHD ADL ADPKD ADS AENNS AFB AFI AFO AFP AGA AGCUS AH4 AH5 AH6 AIIS AKA AL ALG AML AMO ANCA AO APD APGAR APSGN AQ AR ARDS ARF ARM AROM with each word in each line – Vaishali Mar 12 '16 at 07:13
  • I am not using JSON object. I am using array to assign the value. Still I didn't get solution – Vaishali Mar 12 '16 at 12:02
  • @Vaishali My code uses a text file EXACTLY like yours (ABCD ABG ABI ABO AC A...) with each value on a new line and shows how to properly access the results of the resulting dictionary... with Object.keys(). The second part of my answer SUGGESTS using JSON and shows how (I know that's not what you are using now). – Scott Marcus Mar 12 '16 at 14:20
  • @Vaishali I have a working example of the code above located on my server and accessible at: http://techtrainsolutions.com/so/index.html Open the console when you run it to see what's being returned and how I'm accessing it. The example makes 2 AJAX calls. The first is identical to your situation and the second shows how JSON could be used. Feel free to also look at: http://techtrainsolutions.com/so/SpellCheck.txt (which is the first set of data queried) and http://techtrainsolutions.com/so/SpellCheck2.txt (which shows the data in JSON form). – Scott Marcus Mar 12 '16 at 14:47