JavaScript Access Array with String key access array with pointer c++,access array with pointer,access

I have two variables with JSON files. The first is a list of keys looks like this:

keylist = ["key1","key2","key3"]

The second one is generated from a database and looks like this:

data = {
    "key1"{
         #further data
    },
    "key2"{
        #further data 
    },
    "key3"{
        #further data
    }
}

Now I want to access the second element of the database with the key from the keylist

data.keylist[1];

Which doesn't work because the return of keylist[1] is a String? I did some research and the use of the window function was proposed. So I tried this:

window["data." + keylist[1]]();

Which leads to a "is not a function" error. What can I do to solve this problem?

Answer:1

As simple as that:

const mydata = data[ keylist[1] ];

Also, your code is correct from the point of syntax, but it tells completely different than you expect it to tell.

data.keylist[1];

tells JS that you expect to have an object called data which has a property called keylist and which is (most likely) type of array, and you want to get the second element of this array.

PS: And one more point here. Your question title is not completely correct because of the difference between Arrays and Object in JS.

There is no "string keys" for arrays in JS, so you cannot "access array with a string key". Well, truly speaking there are, but not for items of array. Array items only have numeric index, which you can use to access it. Objects, in contrast to arrays, may have named properties. So when you see something like that: data = myVar['data'], you can tell that you're dealing with an object, while data = someVar[0] can be both, an Array (most likely) or also an Object with key named '0'.

Answer:2

I don't think the issue you're having with your first example is because it returns a key. I believe the issue is because data doesn't have a property called keylist. Instead of that, try it as

data[keylist[1]]

and see if that works for you. The reason this one should work is that, in this situation, Javascript will evaluate the string return of keylist[1] and then use it as a string index for the data variable. Let me know if this works out for you :D

Answer:3

I have an array like below and I want to extract objects and sub-array and create a new array if the selected object value is true menu: [ { category_name: "snacks", selected: true ...

I have an array like below and I want to extract objects and sub-array and create a new array if the selected object value is true menu: [ { category_name: "snacks", selected: true ...

I have included javascript for dynamically adding input fields but the first time its clicked it doesn't style correctly. Here is how it looks before utilizing add button (ignore the Author 1/...

I have included javascript for dynamically adding input fields but the first time its clicked it doesn't style correctly. Here is how it looks before utilizing add button (ignore the Author 1/...

  1. dynamic adding new input field
  2. dynamic adding new input field ionic

I have been struggling to include a profile picture upload along with regular text data and send that all to the back end to create a new user through mongoose. I have tried everything from ng-file-...

I have been struggling to include a profile picture upload along with regular text data and send that all to the back end to create a new user through mongoose. I have tried everything from ng-file-...

  1. angular image upload with preview
  2. angular image upload component
  3. angular image upload npm
  4. angular image upload example
  5. angular image upload stackblitz
  6. angular image upload with crop
  7. angular image upload base64
  8. angular image upload library
  9. angular image upload validation
  10. angular image upload plugin
  11. angular image upload made easy
  12. angular image upload with spring boot
  13. angular image upload with preview example
  14. angular image upload firebase
  15. angular image upload .net core
  16. angular image upload and display
  17. angular image upload and retrieve with firebase storage
  18. angular image upload formdata
  19. angular image upload drag and drop
  20. angular image upload with form data

In my code, the x value is undefined. If I remove if block, the x value is displayed as 77. I don't understand why if block is modifying the x value. var x = 77; function fn() { if (false) {...

In my code, the x value is undefined. If I remove if block, the x value is displayed as 77. I don't understand why if block is modifying the x value. var x = 77; function fn() { if (false) {...

  1. javascript hoisting functions
  2. javascript hoisting why
  3. javascript hoisting let
  4. javascript hoisting quiz
  5. javascript hoisting const
  6. javascript hoisting es6
  7. javascript hoisting stack overflow
  8. javascript hoisting questions
  9. javascript hoisting interview questions
  10. javascript hoisting medium
  11. javascript hoisting mdn
  12. javascript hoisting tutorial
  13. javascript hoisting variables
  14. javascript hoisting definition
  15. javascript hoisting meaning
  16. javascript hoisting not working
  17. javascript hoisting advantages
  18. javascript hoisting order
  19. javascript hoisting problem
  20. javascript hoisting inside function