I have a simple program like:
var a = {\'a\': 1, \'b\': 2}
console.log(a)
console.log(a instanceof Array)
console.log(a.constructor instanceof Array)
To be more rigorous you can use JSON. But it's not a performance & memory efficient solution:
function isJsonable(v) {
try{
return JSON.stringify(v) === JSON.stringify(JSON.parse(JSON.stringify(v)));
} catch(e){
/*console.error("not a dict",e);*/
return false;
}
}
So the answer can be: (note that it is an inefficient method and is recommended for test purposed only)
function isDict(v) {
return !!v && typeof v==='object' && v!==null && !(v instanceof Array) && !(v instanceof Date) && isJsonable(v);
}
function isDict(v) {
return typeof v==='object' && v!==null && !(v instanceof Array) && !(v instanceof Date);
}
I use the toString method in Object.prototype, This works like a charm in all the cases(Array,null,undefined etc).
var array_var=[1,2];
var dict_var={
'a':'hey',
'b':'hello'
};
console.log(Object.prototype.toString.call(dict_var) === '[object Object]');//returns true
console.log(Object.prototype.toString.call(array_var) === '[object Object]');//returns false
In short the toString method is used to represent the object, for example the toString method for an array returns '[object Array]'
The structure {'a': 1, 'b': 2}
is a Javascript object. It can be used sort of like a dictionary, but Javascript does not have an actual dictionary type.
console.log(typeof a); // "object"
console.log(Array.isArray(a)); // false, because it's not an array
If you want to know if something is an array, then use:
Array.isArray(a)
If you want to know if something is an object, then use:
typeof a === "object"
But, you will have to be careful because an Array is an object too.
If you want to know if something is a plain object, you can look at what jQuery does to detect a plain object:
isPlainObject: function( obj ) {
// Not plain objects:
// - Any object or value whose internal [[Class]] property is not "[object Object]"
// - DOM nodes
// - window
if ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
return false;
}
// Support: Firefox <20
// The try/catch suppresses exceptions thrown when attempting to access
// the "constructor" property of certain host objects, ie. |window.location|
// https://bugzilla.mozilla.org/show_bug.cgi?id=814622
try {
if ( obj.constructor &&
!hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) {
return false;
}
} catch ( e ) {
return false;
}
// If the function hasn't returned already, we're confident that
// |obj| is a plain object, created by {} or constructed with new Object
return true;
},
Would that work ?
function isDictObj(obj: any) {
try {
const test = {...obj}
} catch(err) {
return false
}
return true
}
My logic is that if it is a dictionnary object (key:values), then using spread operator with {} should work, otherwise throw an exception
The simplest approach to check if something is a dictionary in Javascript in a way that will not also return true
when given an array is:
if (a.constructor == Object) {
// code here...
}
This was inspired by the answer here.