1

I have an array that looks like this:

var array = [[10, 8, 2], [5, 7, 1], [3, 9, 4]];

I need something that tells me if the sub object exists, without throwing an error. Here's an example:

elementExsits(array[0][4]);
//false

elementExsists(array[1][2]);
//true

The function elementExsists would verify it the path exists. I've tried:

if (typeof array[3][2] !== 'undefined') {};

But it just says

Cannot read property '2' of undefined

6 Answers 6

4

You check one property at a time. Stop if you encounter undefined:

if (typeof array[3] !== "undefined" && typeof array[3][2] !== "undefined") {
}

Or better:

if (3 in array && 2 in array[3]) {
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is a very compact way of doing it and works well.
2

A simple if statement can decide if an element exists:

var array = [[10, 8, 2], [5, 7, 1], [3, 9, 4]];

if (array[1] && array[1][2]) {
  console.log('exists');
} else {
  console.log('doesn\'t exist');
}

if (array[10] && array[10][2]) {
  console.log('exists');
} else {
  console.log('doesn\'t exist');
}

Trying to access an array value that doesn't exist returns undefined, so if statements will switch based on the result.

2 Comments

Try the same idea on array[10][1].
"works" only if the array doesn't contain falsy values like 0 developer.mozilla.org/en-US/docs/Glossary/Falsy
2

Salman's answer works for your scenario. Or, you can also create a dynamic function to return the value at the index, or undefined if not found.

var array = [[10, 8, 2], [5, 7, 1], [3, 9, 4]];

function elementExists(array, ...indices){
    return indices.reduce((el, i) => el && el[i], array)
}

console.log(elementExists(array, 0, 4))
console.log(elementExists(array, 1, 2))
console.log(elementExists(array, 3, 2))

1 Comment

@SalmanA Yes. For example .. array = [[10, [1,[11,12]],8, 2]] and elementExists(array,0,1,1,0) gives you 11. (Four levels deep).
1

You could use in operator and check if the index exists. Then proceed with the next nested array.

This solution works with the array and an array of indices and uses a short circuit with Array#every, if the index is not in the given array.

function elementExsists(array, indices) {
    return indices.every(i => Array.isArray(array) && i in array && (array = array[i], true))
}

var array = [[10, 8, 2], [5, 7, 1], [3, 9, 4]];

console.log(elementExsists(array, [0, 4]));  // false
console.log(elementExsists(array, [1, 2])); //  true

1 Comment

Thanks for taking the time to answer the question. Really appreciated.
0

You can take an array, and a list of indexes, and using Array.every() iterate the indexes. If all indexes are not undefined the element exists. If event one is undefined, the element doesn't exist.

const elementExists = (array, ...indexes) => {
  let current = array;
  
  return indexes.every(index => {
    current = array[index];
    
    return current !== undefined;
  });
};

const array = [[10, 8, 2], [5, 7, 1], [3, 9, 4]];

console.log(elementExists(array, 0, 4)); // false

console.log(elementExists(array, 1, 2)); // true

console.log(elementExists(array, 3, 2)); // false

Comments

0

function elementExists(a, i, j) {
  return Array.isArray(a) && Array.isArray(a[i]) && a[i].length > j;
}

console.log(elementExists(null, 0, 1));
console.log(elementExists(undefined, 0, 1));
console.log(elementExists(0, 0, 1));
console.log(elementExists('foo', 0, 1));
console.log(elementExists([], 0, 1));
console.log(elementExists([null], 0, 1));
console.log(elementExists([0], 0, 1));
console.log(elementExists([[]], 0, 1));
console.log(elementExists([[],[]], 0, 1));
console.log(elementExists([[3],[9]], 0, 1));
console.log(elementExists([[3,2],[9]], 0, 1));

1 Comment

Thanks for taking the time to post this answer. Looks like it's extremely universal.

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.