-2

i have an object on state that is inside of an array, for purposes that make me want to cry (not really but i am sure you get what i mean). i need to take the object outside of the array and assign one of the keys to a variable to pass it on a query (the query part is just background info, not important to the question).

0   {…}
id  1
dentist dentist_test
date    1/26/2018
time    6543
avalable    false
name    dentist_test
office  test_office

this is what is on state but like i said it is also in an array.

i want something like this

var id = what ever id is on state
vadzim dvorak
  • 939
  • 6
  • 24
Devin Bowen
  • 89
  • 2
  • 9
  • 1
    Could you put this array here in json format? Do you want key by value? – Rauli Rajande Jan 18 '18 at 23:34
  • https://stackoverflow.com/questions/9907419/how-to-get-a-key-in-a-javascript-object-by-its-value – Rauli Rajande Jan 18 '18 at 23:35
  • What did you try? There are many ways to do it. Keeping your code less pseudo would help others understand what you mean, it's unclear what you are asking and what the contents of the array are, why didn't use real JavaScript? Instead of a long winded paragraph, show the code and what you have tried, what happens instead of the expected behavior? – Ruan Mendes Jan 18 '18 at 23:41
  • Right now it looks like you should be able to use `var id = arr[0].id`, but the question is unclear – Ruan Mendes Jan 18 '18 at 23:45

1 Answers1

1

You can dynamically name a property (key) like:

var myName = "foo";
var obj = {};
obj[name] = myName;  
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91
  • Unclear why you think this is the OP's problem, they didn't mention anything about a dynamically named property. I guess if the format of the array is that each element is an array tuple, then the property name would be a string, but OP said I have an object in an array, not an array inside the array – Ruan Mendes Jan 18 '18 at 23:49
  • he said he needed to pass a key from a variable... Maybe I misunderstood – Ronnie Royston Jan 19 '18 at 04:29
  • OP said they needed to pass a key into a variable `and assign one of the keys to a variable` not a variable into a key. I know you're just trying to help, but people need to learn to ask better questions so those who like to help can do so without decrypting a question ;) – Ruan Mendes Jan 19 '18 at 19:00