/    Sign up×
Community /Pin to ProfileBookmark

Object Manipulation – Counting Parameters?

Hi there.
Let’s say I get a user-custom JavaScript object, with an unknown number of properties, all of them having custom, unknown names:

[code]
var object = {
‘foo’: 1,
‘bar’: 2,

‘baz’: n
}
[/code]

Now, let’s say I would like to count the number of properties I have inside the object – can I do it? And if so, how?

Plus, can I find out in some way what are the names of the properties, so I could use them?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@toicontienMay 04.2007 — Try this function out:
/**
* @function count
* Counts the number of properties in an object, or elements in an array,
* or characters in a string.
*
* @param x (variable, required)
* The variable whose elements are to be counted.
*
* @param countFns (boolean, optional)
* Assumed to be false. When true, object member functions will be
* counted. When false, the returned count only applies to string, object,
* array and number variables.
*
* @return number
* The number of properties in the object, number of elements in an array,
* or number of characters in a string. If a function or number variable
* type was passed as the first parameter, a null value is returned.
*/
function count(x, countFns) {
var num = 0, prop = null;
if (x.constructor == Object) {
for (prop in x) {
if (countFns || (!countFns && typeof(x[i]) !== 'function')) {
num++;
}
}
} else if (x.constructor == Array) {
num = x.length;
} else if (x.constructor == String) {
num = x.length;
} else {
num = null;
}
return num;
}


You can call it like this:
var obj = {
foo: 1,
bar: 2,
myFn: function() {},
baz: 3
};

var myarray = ['a','b','c','d'];
var mystring = 'WTF mate?';

alert(count(obj)); // Alerts 3
alert(count(obj, true)); // Alerts 4, counts function
alert(count(myarray)); // Alerts 4
alert(count(mystring)); // Alerts 9
Copy linkTweet thisAlerts:
@pilauauthorMay 04.2007 — Thanks, that's awesome!

Now, please, can I get the names of the properties as well?
Copy linkTweet thisAlerts:
@mrhooMay 04.2007 — Usually you do not want to include the prototype methods when

you get an object's members- this weeds them out and returns an array of property names.

The length of the array is the count.


[CODE]function rollcall(obj){
if(this.constructor==Array)return this;
var A=[];
for(var p in this){
if(p in this.constructor.prototype) continue
A.push(p);
}
return A
}[/CODE]


invoke it on an object instance with rollcall.call(obj);
Copy linkTweet thisAlerts:
@pilauauthorMay 04.2007 — It is still a bit obscured for me. Let me try and explain to you what I'm trying to code.

I want to make a function that recieves a div_element reference, plus a parameters object from a user, which in turn contains style atributes and values, in this form:

[CODE]
function css(div_element, {attribute1: value1, attribute2: value2, attribute3: value3});
[/CODE]


I'd like the function to set the style attributes to the div_element.

A proper call to the function would be, say:
[CODE]css(div, {background-color: '#123456'});[/CODE]

And then, the function will set the background-color attribute of the div to the given value.

For this I have to get the property names from the parameter object.
Copy linkTweet thisAlerts:
@mrhooMay 04.2007 — [CODE]function css(div_element, obj){
for(var p in obj){
try{
div_element.style[p]=obj[p];
}
catch(er){
//try next p
}
}
return div_element;
}[/CODE]


if you are getting user defined objects, the try block is a good idea, but you can skip it if you trust the input.

[CODE]function css(div_element, obj){
for(var p in obj) div_element.style[p]=obj[p];
return div_element;
}[/CODE]
Copy linkTweet thisAlerts:
@pilauauthorMay 04.2007 — Thanks, this is exactly what I needed!
×

Success!

Help @pilau spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...