1

I wanted to write a little helper function to return 0 if the js array raises undefined for a nested element in an array. The problem i have is that if the first index already fails and raises undefined error. The nested index is not going to fail at the second index which should! But it's not getting there. How would it be best to catch all errors here?

function helper_check(arr){
  if (typeof(arr) == 'undefined') {
    return 0
 }

var some_var = helper_check(some_arr[0][1]) // so here some_arr[0] already fails and raises an error but i want to catch all errors witht he helper function

2 Answers 2

1

I think you are trying to do something like this. Here if arr is an empty array so it will fail on the second if. If arr is not an array for some reason it will fail on the first if.

function get_val(arr, x, y){
   if (!arr || !Array.isArray(arr)) {
     return 0;
   }

   if (arr[x] === undefined || arr[x][y] === undefined) {
    return 0;
   }

   return arr[x][y];
}

var some_var = get_val(some_arr, 0, 1)

If you want to keep the old approach you must catch the error.

function helper_check(arr){
  if (typeof(arr) == 'undefined') {
    return 0
 }

var some_var = 0;
try {
    some_var = helper_check(some_arr[0][1]);
}
catch(err) {//nothing to do here}

Another less elegant option

function helper_check(arr){
  try {
     var val = eval(arr);
     if (typeof(val) == 'undefined') {
        return 0;
     }
     return val;
  catch(err) {
     return 0;
  }
 }

some_var = helper_check('some_arr[0][1]');
Sign up to request clarification or add additional context in comments.

5 Comments

thanx for the answer. But to hand in the indexes also is to my opinion quite unelegant... have an example where i could omit the two more parameters?
You cannot go the way you want. When doing this. helper_check(some_arr[0][1]) , some_arr[0][1] is an expression which is resolved before passing the data to helper_check. The only thing you can do is to catch any error and return 0 in case. I will make a small edit
okay let's see if someone else having another idea. if not i will accept your answer... thx
Hey I added another less elegant option. Note the string type. However that is the closest to what you were trying to do.
like! Less elegant but less to write ;-P
0

This is an older question but I came across it when I had a similar problem, and I am posting my answer here in case it can help someone else.

typeof(arr) == 'undefined'

This approach was not actually catching the undefined state of [arr] in my code. I also tried the try/catch approach https://www.w3schools.com/js/js_errors.asp with a similar result.

The solution, which worked for me, was oddly simple. Just test the length property of [arr].

if (arr.length < 1)

Depending on if you want to be more explicit there are a variety of other options along this line of thinking.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.