Javascript count keys in braces of function

307 Views Asked by At

I call a function like so:

update_insert('information',{
    element_id: 1,
    type: 'menu',
    info: 'Hi how are ya?',
    new_window: ''
});

The function is:

function update_insert(table,data){
    alert(data.length);
}

I am trying to get the number of keys that I inserted into the function, and eventually get the names of the keys, dynamically with a loop. How can I achieve this?

6

There are 6 best solutions below

4
c.hill On BEST ANSWER

Objects don't have the .length attribute like arrays do in JavaScript. You'll need to use a function to count the number of items in an object:

getObjectLength = function(obj) {

    var size = 0, key;

    for (key in obj) {
        if (obj.hasOwnProperty(key))
            size++;
    }

    return size;
}

EDIT:

And to get the keys from an object:

getObjectKeys = function(obj) {

    var keys = [];

    for (var key in obj) {
        keys.push(key);
    }

    return keys;
}
3
alex On

You can get the keys with the following function (which is safe to use in older browsers)...

var getObjectKeys = function(obj) {

    if (Object.keys && typeof Object.keys == "function") {
        return Object.keys(obj);
    }

    var prop, keys = [];

    for (prop in obj) {
        if (obj.hasOwnProperty(prop)) {
            keys.push(prop);
        }
    }

    return keys;
}

jsFiddle.

Alternatively, use Object.keys() and use an equivalent [shim][2] for older browsers.

You can check the length property of the returned Array to determine the amount of keys.


If you want to dredge up all enumerable properties on the prototype chain, you can use...

var getObjectKeysIncludingInherited = function(obj) {
    var keys = [],
        i = 0;

    for (keys[i++] in obj);

    return keys;
}

jsFiddle.


Alternatively, you may really want to use the length property of an object to do it, but please don't. It's considered somewhat dangerous to augment native JavaScript objects, and it's kind of confusing (Objects don't have a length property but Arrays do) and it won't work in older browsers...

Object.defineProperty(Object.prototype, "length", {
    get: function() {
        return Object.keys(this).length;
    }        
});

jsFiddle.

0
Elliot Bonneville On

Something like this, perhaps?

function update_insert(table,data){
    var dataLength = 0, keys = [];

    for(prop in data) {
        if(data.hasOwnProperty(prop)) {
            dataLength++;
            keys.push(prop);
        }
    }
}

Arrays are actually objects which have a custom length property (and some other stuff :P) so not all objects have a length property.

0
web_bod On
var names = new Array();

for(name in data)
{
  if(data.hasOwnProperty(name))
  {
    names.push(name); 
  }
}

names.length - will give you what you need.
0
hasrthur On
var keys = [];
for(var k in data) {
  if(Object.prototype.hasOwnProperty(data, k)) {
    keys.push(data[k]);
  }
}
k.length //size of keys
0
German Latorre On

In case you are a jQuery fan or potential user:

var size = 0;

$.each(obj, function(key, value) {
    alert('key = ' + key);
    alert('value = ' + value);
    size++;
});

alert(size);