I'm trying to get the keys from a JavaScript Object. When I inspect the object I can see that there is a key value pair on the object but when I run Object.keys(myObject)
I get an empty array returned. What am I doing wrong?
I'm following this documentation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
And the function which creates the object is as follows:
query: function () {
var query = {}
_.each(this.$el.find('input:checked'), function (el, index) {
Object.defineProperty(query, el.id, {value: el.value})
})
return query
}
Object.keys
only returns enumerable own keys. Try Object.getOwnPropertyNames
.
Console.log
prints the most recent value of its contents and not the value of that content at that specific time of execution.
To log the array, try deep-cloning its value to another variable and then console log it.
console.log(JSON.parse(JSON.stringify(keys)));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With