How to determine if a javascript object is simple or complex?

Basically I need to talk about all of the following two:

var simple = 5 // or "word", or 56.78, or any other "simple" object var complex = {propname: "propvalue", "otherprop": "othervalue"} 
+4
source share
7 answers

Using the typeof operator, you can define the following:

 "number" Operand is a number "string" Operand is a string "boolean" Operand is a Boolean "object" Operand is an object "undefined" Operand is not defined. 

Edited: As suggested in the comment, you can also check if the value is null, since typeof null will return an object.

+7
source

You can use typeof :

 typeof 5 == "number"; typeof 1.5 == "number"; typeof true == "boolean"; typeof "word" == "string"; typeof {} == "object"; 

Basically:

 if(obj == null) { //null or undefined } else if(typeof obj == "object") { //It "complex" } else { //Primitive or "simple" } 

Note: null will return "object" , so you need to check it out.

+2
source

Try to execute

 if (typeof obj === 'object') { // It complex } else { // It not } 
+1
source

Loan here

 Object.prototype.getName = function() { var funcNameRegex = /function (.{1,})\(/; var results = (funcNameRegex).exec((this).constructor.toString()); return (results && results.length > 1) ? results[1] : ""; }; var simple = 5; // or "word", or 56.78, or any other "simple" object var complex = { propname : "propvalue" , "otherprop" : "othervalue" }; 

 simple.getName(); // returns: "Number" complex.getName(); // returns: "Object" 
+1
source

The problem is that more than just {} returns an object type

 typeof 5 == 'number' typeof NaN == 'number' typeof 'test' == 'string' typeof true == 'boolean' typeof undefined == 'undefined' typeof null == 'object' typeof /asdf/ == 'object' // this is true in some engines, like Firefox's. Not in V8 (in which it is 'function') typeof [] == 'object' typeof {} == 'object' 

But using toString, you can additionally check:

 toString.call(null) == '[object Window]' // or '[object global]' or '[object Null]' - depends on engine toString.call(/asdf/) == '[object RegExp]' toString.call([]) == '[object Array]' toString.call({}) == '[object Object]' 

So the best way to check:

 var test; test = {}; typeof test == 'object' && toString.call(test) == '[object Object]'; // true test = []; typeof test == 'object' && toString.call(test) == '[object Object]'; // false // et cetera 

Hope that helps

+1
source

In your case:

 var simple = 5; // number, not an object var simple = new Number(5); // now it is an object, but still the value is 5 var complex = {propname: "propvalue", "otherprop": "othervalue"}; for ( property in complex ) { if ( complex.hasOwnProperty( property ) ) { alert ( 'composite object' ); return; } else { alert ( 'simple object' ); return; } } 

As far as I understand from you the question is - you need to indicate whether the object has properties / methods.

0
source

You can simply create a simple function that returns true for simple types:

 function isSimple( a ) { return (typeof a).match(/(number)|(boolean)|(string)/) } 

Not that it will return true for NaN , as it is considered a number, but false for 'undefined' - but you can easily change this to suit your specific case.

Run the snapshot below to see it in action.

 <script> // return true/false if typeof matches simple regex pattern function isSimple( a ) { return (typeof a).match(/(number)|(boolean)|(string)/); } // setup some tests cases var tests = [ [1,2,3], 'hello', 7, { foo: 'bar' }, NaN ] // log output of isSimple function against each test case for( var i in tests ) { var value = tests[ i ]; if( isSimple( value ) ) { console.log( 'simple value', value ); } else { console.log( 'not simple', value ); } } </script> 
0
source

All Articles