views:

12

answers:

1

Hi,

I am having small problem in my JS where i need to iterate through all the keys in the associative array.

var listOfKeys=new Array();
for (var key in parsedArray)
{
     listOfKeys.push(key);
}

It works perfectly and returns all the properties associated to the object. Now i have a situation to add the prototype method to an array,

Array.prototype.ModifyKey= function(key,value){
        //some code
}

so now the parsedArray is eligible to access this new prototype ModifyKey (actually ModifyKey is a member to all my arrays).

Now when i loop through the parsedArray to find all the keys, it returns all the keys along with the prototypes associated with that...

is there a better way to overcome this..

I know some workarounds like, having the parsedArray as simplearray which holds the array of key value pairs to get the keys without this problem. But the input array am getting is not in my control, its a json result from the another REST service.

Cheers

Ramesh Vel

+1  A: 

You need to use hasOwnProperty:

var listOfKeys=new Array();
for (var key in parsedArray) {
    if (parseArray.hasOwnProperty(key)) {
        listOfKeys.push(key);
    }
}
Gumbo
thanks Gumbo, you save my day.... :)
Ramesh Vel