Javascript - check if object parameter is undefined

I have a situation similar to the situation: Javascript checks if an object property exists, even if the object is undefined .

My problem is what happens if you have a property chain. Example:

var obj = {'a': {'b': {'c': {'d': 'I exists'}}}}

I need to check if 'd' is defined. In order not to get the error, I would need to check:

if (typeof obj != 'undefined' && typeof obj['a'] != 'undefined' && typeof obj['a']['b'] != 'undefined' && typeof obj['a']['b']['c'] != 'undefined' && typeof obj['a']['b']['c']['d'] != 'undefined')

You can see how annoying it can be. For example, extrapolate to a deep element of level 999. Is there a way to get rid of the first n-1 conditions?

+4
source share
6 answers

Solution with try-catch:

var check = function(obj) {
  try {
    return (typeof obj.a.b.c.d !== 'undefined');
  } catch (e) {
    return false;
  }
};

alert(check({
  'a': {
    'b': {
      'c': {
        'd': 'I exists'
      }
    }
  }
}));
Run codeHide result
+4
source

You can check the meaning of truth as follows.

n - 1,

if (obj && obj.a && obj.a.b && obj.a.b.c && typeof obj.a.b.c.d !== 'undefined')
    // Use of obj.a.b.c.d is considered safe here
+1

:

n - 1

, , .

, :

JSFIDDLE

var obj = {'a': {'b': {'c': {'d': 'I exists'}}}};

for (var key in obj)
{
    if (obj && obj.a.b.c.d) 
    { 
        console.log(obj.a.b.c.d);
    }
}
+1

:

function checkUndefined(obj) {
  var x = Array.prototype.slice.call(arguments, 1);
  for (var i = 0; i < x.length; i++) {
    if (!obj || !obj.hasOwnProperty(x[i])) {
      return false;
    }
    obj = obj[x[i]];
  }
  return true;
}
+1

, :

JavaScript:

function test(object) {
    var restOfKeys = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        restOfKeys[_i - 1] = arguments[_i];
    }
    var propertyToTest;
    if (object === undefined || !restOfKeys.length) {
        return false;
    }
    propertyToTest = restOfKeys.shift();
    if (restOfKeys.length) {
        return test.apply(void 0, [object[propertyToTest]].concat(restOfKeys));
    }
    return object[propertyToTest] !== undefined;
}
var toTest = { a: { b: { c: "asd" } } };
alert(test(toTest, "a", "b", "c"));

TypeScript:

function test(object: any, ...restOfKeys: string[]) {
    let propertyToTest: string;

    if (object === undefined || !restOfKeys.length) {
        return false;
    }

    propertyToTest = restOfKeys.shift();

    if (restOfKeys.length) {
        return test(object[propertyToTest], ...restOfKeys);
    }

    return object[propertyToTest] !== undefined;
}


var toTest = { a: { b: { c: "asd" } } };

alert(test(toTest, "a", "b", "c"));

0

var obj = {a:undefined,b:undefined,c:undefined,d:'I exists'};
  if(typeof obj['a'] === 'object'){alert(typeof obj['a']);}
  else if(typeof obj['b'] === 'object'){alert(typeof obj['b']);}
  else if(typeof obj['c'] === 'object'){alert(typeof obj['c']);}
  else{alert(typeof obj['d']+' : '+obj['d']);}
Hide result

http://www.w3schools.com/js/js_datatypes.asp http://www.w3schools.com/js/js_arrays.asp .

0
source

Source: https://habr.com/ru/post/1609781/


All Articles