Jack
Jack

Reputation: 1999

See if subarray of array exists

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

Upvotes: 1

Views: 961

Answers (6)

Nina Scholz
Nina Scholz

Reputation: 386560

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

Upvotes: 1

cybersam
cybersam

Reputation: 66989

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));

Upvotes: 0

rmn
rmn

Reputation: 1169

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))

Upvotes: 2

Salman Arshad
Salman Arshad

Reputation: 272096

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]) {
}

Upvotes: 4

Adam
Adam

Reputation: 3965

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.

Upvotes: 2

Ori Drori
Ori Drori

Reputation: 191976

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

Upvotes: 0

Related Questions