Penghan Yang
Penghan Yang

Reputation: 27

write a function that gets passed an object and returns an array of the object's properties

My homework is like: Write a "keys" function that gets passed an object and returns an array of the object's properties. Be sure to screen out the object's methods. The keys array only has names of the object's name/value pairs. Because of cross browser issues(non-support in older browsers), the Objectkeys method cannot be used. Your function should provide the same service for all the browsers.

My initial code is as below:

function keys(obj){
  var key="";
  var i = 0;
  var array = [];
  for(i = 1; i<arguments.length; i++){
     for(key in arguments[i]){
         if(obj.hasOwnProperty&&(!_.isArray(obj))){
             obj[key]=arguments[i][key];
         }
     }
  }
  for(var j = 0; j < obj.length; j++){
     for(key in obj[j]){
        array[j] = obj[j];
     }
  }
 return array;
 }

I'm pretty sure that my function has a lot of problems. Could you please help me with it? Thank you!

Upvotes: 0

Views: 401

Answers (3)

Aadit M Shah
Aadit M Shah

Reputation: 74204

This is the solution:

function keys(obj) {
    var hasOwnProperty = Object.prototype.hasOwnProperty;

    var properties = [];

    for (var property in obj)
        if (hasOwnProperty.call(obj, property)
            && typeof obj[property] !== "function")
                properties.push(property);

    return properties;
}

Line by line the above code does the following:

  1. Create an empty array properties to hold the names of all the properties of obj.
  2. For each property property of obj do:
    1. If the property property belongs to obj and
    2. If obj[property] is not a function then:
      1. Add the property property to the properties array.
  3. Return the properties array.

See the demo: http://jsfiddle.net/qVgVn/

Upvotes: 1

Muhammad Umer
Muhammad Umer

Reputation: 18097

ok here i go...

function objProps(x){
var arr=[];
for (var k in x) if(typeof x[k] !='function' && x.hasOwnProperty(k)) {arr.push(k);}
return arr;
}

this code works as expected. call it with object...

get its only keys out that are NOT Functions.

Upvotes: 1

netpoetica
netpoetica

Reputation: 3425

There are a lot of problems with your code. The answer you need is here in the MDN though: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

That function does EXACTLY what your professor asked, and it does it cross-browser. There is what is called a poly-fill, or cross-browser implementation of object.keys listed under "Compatability". Try to sort out that code to figure out what it's doing :)

Here are some problems with your own code that I see right off the bat - it's probably not working code, I just wanted to give you some guidance of things you did incorrectly:

// Name your function something useful and descriptive.
function getKeysAsArray(obj){
  // For starters, dont name a variable "array" - bad practice.
  var key="",
      i = 0,
      results = [];

  // First foor loop unnecessary, do not use arguments here
  // because you already know the name of your argument.
     for(key in obj){
         // See if this browser supports has OwnProperty by using typeof
         // which will fail gracefully, vs what u did which will stop the
         // script from running
         if(typeof Object.hasOwnProperty === 'function'){
             // You probably shouldn't be using underscore _
             if(obj.hasOwnProperty && !(obj instanceof Array)){
                 results.push(obj[key]);
             }
         }
     }

  return results;
}

Upvotes: 1

Related Questions