Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using typeof() to get object value type – receiving string instead of array

I have an object where two parameters hold arrays as their values. When I try to output the types of these values using the typeof() function in a loop, for some reason, I always get a string type instead of the actual array value.

const add = "add"
const edit = "edit"
const required = {
  user: [add, edit],
  profile: [edit],
}

for (let p in required) {
  console.log(p, typeof(p))
}
Output:

string
string

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is how you access the array (the value)-

Object[key] which translates, in your case to required[p].

(The typeof function logs arrays as 'objects')

const add = "add";
const edit = "edit"
const required = {
  user: [add, edit],
  profile: [edit]
}

for (let p in required) {
  console.log(required[p])
}

for (let p in required) {
  console.log(typeof(required[p]))
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading