How would I write the code below as function and not using the built in Object.keys() method? Thank you!
var obj = { Name: 'Joe', Age: 18, pass: true };
console.log(Object.keys(obj)); // => [ 'Name', 'Age', 'pass' ]
How would I write the code below as function and not using the built in Object.keys() method? Thank you!
var obj = { Name: 'Joe', Age: 18, pass: true };
console.log(Object.keys(obj)); // => [ 'Name', 'Age', 'pass' ]
Do you mean this?
function getKeys(obj){
return Object.keys(obj)
}
or
function getKeys(){
var keys = [];
for (var key in foo) {
keys.push(key);
}
return keys;
}
or you can do
Object.prototype.getKeys = function(o, f, ctx) {
return Object.keys(o);
}
// obj.getKeys() => [ 'Name', 'Age', 'pass' ]
@Shayan's response here is what I was looking for:
function getKeys(){
var keys = [];
for (var key in foo) {
keys.push(key);
}
return keys;
}