less than 1 minute read

Checking a key without error cases

Before proceeding to the main topic, i would briefly summarize the object type check method.’

Using a typeof operator

This is the method checking the types of objects contained in a variable. There is a simple example refereced in this site

class Typetester {
    void printType(byte x) {
        System.out.println(x + " is an byte");
    }
    void printType(int x) {
        System.out.println(x + " is an int");
    }
    void printType(float x) {
        System.out.println(x + " is an float");
    }
    void printType(double x) {
        System.out.println(x + " is an double");
    }
    void printType(char x) {
        System.out.println(x + " is an char");
    }
}
Typetester t = new Typetester();
t.printType( yourVariable );

Checking a key without error cases

Below is the safe-code so that an error does not occur when checking the key value for a null value. In this case, functional programming was used.

function _obj_check(obj) { retrun typeof obj == 'object' ;}
function _keys(obj) {return _obj_Check(obj)? object.keys(obj):[]; }

The second function above prevents an error situation by outputting an empty array when the key value does not exist.