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 :
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]))
}