Skip to content Skip to sidebar Skip to footer

Javascript Check If Dictionary

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) Here value of a is dictionary. I

Solution 1:

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.

Solution 2:

functionisDict(v) {
    returntypeof v==='object' && v!==null && !(v instanceofArray) && !(v instanceofDate);
}

Solution 3:

The structure {'a': 1, 'b': 2} is a Javascript object. It can be used sort of like a dictionary, but a Map object is closer to what most people think of as a dictionary.

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// - windowif ( jQuery.type( obj ) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
        returnfalse;
    }

    // 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=814622try {
        if ( obj.constructor &&
                !hasOwn.call( obj.constructor.prototype, "isPrototypeOf" ) ) {
            returnfalse;
        }
    } catch ( e ) {
        returnfalse;
    }

    // If the function hasn't returned already, we're confident that// |obj| is a plain object, created by {} or constructed with new Objectreturntrue;
},

Solution 4:

You can use this to check either your dict(data) is a dictionary or not !

var dict = { a: 1, b: { c: 3, d: 4 }, e: 9 };

// this function will true / falseconstisDict = dict => {
  returntypeof dict === "object" && !Array.isArray(dict);
};

console.log(isDict(dict));  // true

this will return you true if it is a dictionary otherwise return false

Solution 5:

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 trueconsole.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]'

Post a Comment for "Javascript Check If Dictionary"