0

I have a for loop that adds properties to the data object

words is an array of strings

for (let i = 0; i < words.length; i++) {
  let word = words[i];
  data[word] = i;
}

After that, my objext looks like this:

data = {"the": 0, "of": 1, "and": 2, "to": 3, "a": 4, …};

I have tried

data['the'];

but it returns undefined

How do I retrieve the value of the property?

Here is my full code

let vocab = {};

$.ajax({
  url: "10000words.txt",
  success: function(data) {
    data = data.split(/\n/).slice(0, 10000);
    for (let i = 0; i < data.length; i++) {
      let word = data[i];
      vocab[word] = i;
    }
    console.log(vocab['test']);
  }
});

When I type vocab into the console, it prints out vocab but when I try vocab['the'] it returns undefined

bakely
  • 61
  • 14
  • 4
    `data['the']` works fine. You're doing something else wrong. Please provide an https://stackoverflow.com/help/mcve – SLaks Jul 19 '18 at 23:33
  • You wouldn't be trying to do this in an async callback like a http request, would you? – Mark Jul 19 '18 at 23:34
  • before your loop, var data = {}; – Ted Fitzpatrick Jul 19 '18 at 23:36
  • @Mark Meyer I add to the data object with an http request then I try `data['the']` but it is still undefined – bakely Jul 19 '18 at 23:37
  • if you do:`var data = { "the": 0, "of": 1, "and": 2, "to": 3, "a": 4}; var result = data['the'];` you will see that it works fine. Maybe you miss some variable initialisation or something else. – Marlon Assef Jul 19 '18 at 23:40
  • @Marlon Assef that is what im doing – bakely Jul 19 '18 at 23:41
  • @MarkMeyer what is my issue? – bakely Jul 19 '18 at 23:42
  • 1
    Are you trying to access `vocab` outside the ajax function? – Mark Jul 19 '18 at 23:42
  • Yes, I have it declared outside the ajax – bakely Jul 19 '18 at 23:44
  • 1
    @bena The question was whether you’re trying to _access_ `vocab` outside the `success` callback, not whether you’ve declared it outside. Your issue is impossible to reproduce, if you didn’t actually access `vocab` outside of `success`. That’s why I think the most likely issue is this: [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Sebastian Simon Jul 19 '18 at 23:45
  • @Xufox, the call to console.log is in the callback. – Kaiido Jul 19 '18 at 23:47
  • @Kaiido Read the edited comment. It’s just the most likely scenario. – Sebastian Simon Jul 19 '18 at 23:47
  • @Xufox The async function is not the issue – bakely Jul 19 '18 at 23:48
  • @bena The question is either a duplicate, or it’s caused by a typo. Again, the issue is impossible to be reproduced with the code you’ve shown. Try using `debugger`, try logging `vocab` by itself. – Sebastian Simon Jul 19 '18 at 23:50
  • 1
    @bena Actually… try `console.log([...Object.keys(vocab).find((word) => /t[^a-z]*h[^a-z]*e/.test(word))]);` and see whether it matches `["t", "h", "e"]`. There may be invisible characters within your words, thus `vocab.the` won’t work. – Sebastian Simon Jul 20 '18 at 00:17
  • 1
    Your words probably have trailing spaces. – SLaks Jul 20 '18 at 01:18
  • @SLaks You are correct, thank you it works now – bakely Jul 20 '18 at 14:51

1 Answers1

1

Here is an example of how to get a value by the object key.

const words = ['Hello', 'world'];
const data = {};

for(let i = 0; i < words.length; i++) {
  const word = words[i];
  data[word] = i;
}

const index = data['Hello'];

document.write(index);
Roman Mahotskyi
  • 4,576
  • 5
  • 35
  • 68