Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript find in array cannot read property of undefined

function getSafe(fn, defaultVal) {
  try {
    return fn();
  } catch (e) {
    return defaultVal;
  }
}

// use it like this
console.log(getSafe(() => obj.a.lot.of.properties));

// or add an optional default value
console.log(getSafe(() => obj.a.lot.of.properties, 'nothing'));
 Run code snippet
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #javascript #find #array #read #property #undefined
ADD COMMENT
Topic
Name
6+5 =