Determining if a Javascript object is a "complex" object or just a string

I want to be able to pass a string literal,

'this is a string' 

or javascript object,

 {one: 'this', two: 'is', three: 'a', four: 'string' } 

as an argument to a function and take different actions depending on whether it is a string or an object. How to determine what is true?

To be specific, I want to iterate over the properties of an object and parse if the property is a string, but recursively nested if the property is an object. I figured out how to use $.each() to iterate over the properties of an object, but if I just do it with a string, it treats the string as an array of letters, and not as one thing. Can I get around this differently?

+7
javascript jquery string object
source share
6 answers
 var data = { foo: "I'm a string literal", bar: { content: "I'm within an object" } }; 

JQuery

 $.each(data, function(i, element){ if($.isPlainObject(element){ // we got an object here } }); 

JQuery lib has similar methods like $.isArray() or $.isFunction() .

Native Javascript

 for(var element in data){ if(toString.call(element) === '[object Object]'){ // we got an object here } } 

To use the hack'ish method with toString has the advantage that you can determine if it is really object and an array . Both objects and arrays will return object using typeof element .

In short, you cannot rely on the typeof operator to distinguish between true objects and arrays . For this you need toString.call() . If you just need to know if it's an object or not, typeof just fine.

+6
source share
 var a = 'this is a string'; console.log(typeof a); // Displays: "string" var b = {one: 'this', two: 'is', three: 'a', four: 'string' }; console.log(typeof b); // Displays: "object" 

Thus:

 if (typeof yourArgument === 'string') { // Do the string parsing } else if (typeof yourArgument === 'object') { // Do the property enumeration } else { // Throw exception } 

UPDATE:

Some additional considerations:

  • See @Andy E comment below.

  • typeof null returns "object" . The same applies to any other object, including arrays.

+5
source share

Try the following:

 function some_function(argument) { if (typeof(argument) == "string" || argument.constructor == String) { // it a string literal } else if (argument && typeof(argument) == "object" && argument.constructor != Array) { // it an object and not null } else { // error } } 

Thanks to Andy E for tipp with argument.constructor .

+4
source share

Try the typeof statement. It will return object for objects and string for strings.

+2
source share

you can do something like this

 function something(variableX){ if (typeof(variableX) === 'object'){ // Do something }else if (typeof(variableX) === 'string'){ // Do something } } 
+1
source share

I had a similar problem and I think I solved the solution. Here is my sample code for anyone interested.

 var propToDotSyntax = function (obj) { var parse = function (o, n) { var a = [], t; for (var p in o) { if (o.hasOwnProperty(p)) { t = o[p]; if (n !== undefined) tmp = n + '.' + p; else tmp = p; if (t && typeof(t) === 'object') a.push(arguments.callee(t, tmp)); else a.push(tmp + '=' + t); } } return a; }; return parse(obj).toString(); } var i = { prop: 'string', obj: { subprop: 'substring', subobj: { subsubprop: 'subsubstring' } } }; propToDotSyntax(i); 

This will go through all the properties of the object - even if the properties themselves are objects - and return a string with the following values ​​in dot syntax.

 "prop=string,obj.subprop=substring,obj.subobj.subsubprop=subsubstring" 

I got inspiration from DavidPirek.com - Thanks, Mr. Pyrek!

+1
source share

All Articles