0

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?

Chris
  • 44,602
  • 16
  • 137
  • 156
Ilya Karnaukhov
  • 3,838
  • 7
  • 31
  • 53

6 Answers6

4

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;
}
c.hill
  • 3,127
  • 25
  • 31
1

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.

alex
  • 479,566
  • 201
  • 878
  • 984
  • to get the count of keys...use `Object.keys(data).length` – Parth Thakkar May 17 '12 at 13:57
  • Hey. Thanks for all the help guys. I you answered so quickly! In the meantime I found exactly what i was looking for: $.each(data, function(index, value) { alert(index + ': ' + value); }); – Ilya Karnaukhov May 17 '12 at 14:03
  • @IlyaKarnaukhov: Ah, yes, that's a jQuery thing. If you're asking a client-side JavaScript question and you're using jQuery (or another library), be sure to include both the `javascript` tag **and** the tag for the library (in this case `jquery`), so people can point you at something in the library if relevant. – T.J. Crowder May 17 '12 at 14:06
0

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.

Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
0
var names = new Array();

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

names.length - will give you what you need.
web_bod
  • 5,728
  • 1
  • 17
  • 25
0
var keys = [];
for(var k in data) {
  if(Object.prototype.hasOwnProperty(data, k)) {
    keys.push(data[k]);
  }
}
k.length //size of keys
hasrthur
  • 1,410
  • 15
  • 31
0

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);
German Latorre
  • 10,058
  • 14
  • 48
  • 59