1

What's the best way to test for the existence of an array element in a structure several layers deep? For instance:

if (typeof arr[a][b][c] === 'undefined') { ...do something... }

If [a] or [b] don't exist, we won't be able to test for [c].

Are there underscore or lodash functions to handle this?

4 Answers 4

1

It you need it in many places you can create a function:

function get(obj, props) {
  return props.reduce(function(x, p) { return x && x[p] ? x[p] : null }, obj)
}

if (get(arr, [a, b, c])) ...

Works with objects and arrays:

var obj = [0, {a: [0, 'value', 2]}, 2]
get(obj, [1, 'a', 1]) //=> 'value'
get(obj, [1, 'a', 8]) //=> null
Sign up to request clarification or add additional context in comments.

3 Comments

Wouldn't this fail if the values of one of the elements on the path was false?
Yes, I see now: there wouldn't be a false "on the path", because anything but a leaf node would be an array. Only a leaf node could be a false value. Thanks.
Yes, thank you. Brilliant really. Is this the same as the get() in lodash?
1

For sparse array check, just checking an item being undefined is not sufficient. However you can use the in operator to test like

(a,b,c) in arr

var a = [];
a[10] = 1;
b = 10;
console.log(1 in a)
console.log((1,2,3) in a)
console.log(b in a)

6 Comments

Thanks for your answer, but it doesn't address the problem in the question. This will tell you whether all the indexes in list are in the array; but I'm looking for whether the indexes in sequence are in the array. Not individually.
@NotoriousWebmaster No problem.. however i would advise you to rephrase the topic since "sparse array" (arrays with holes in them) means some of it's indices are missing not some of it's items are undefined. So [undefined,3,undefined] is not a sparse array.
so what would a sparse array look like, if not with undefined's strewn about?
@NotoriousWebmaster I would say Array(3) is sparse while Array(3).fill() is not. So on V8 console for instance console.log(Array(3)) would look like [undefined × 3] while Array(3).fill() would look like [undefined, undefined, undefined] which means the first array have no keys (properties) at all while the latter has keys (0,1,2) assigned as undefined as values. A sparse array has holes in the sense of keys. In another words an array with undefined items don't qualify as a sparse array.
In fact, @Redu, I was dealing with a sparse array, where keys had not yet been added, and I needed to see if a given key was there or not. So it was, as per your definition, a sparse array. Thanks for clarifying that for me; I would have thought the undefined one was sparse as well.
|
1

you can do

if (arr && arr[a] && arr[a][b] && typeof arr[a][b][c] === 'undefined') { ...do something... }

or you can create a custom function

function check(arr){
    let temp = arr;
    for(let i=1; i<arguments.length; i++){
        if(temp[arguments[i]] == undefined){
            return false;
        }
        temp = temp[arguments[i]];
    }
    return true;
}


let arr= [[[1], [2]], [3]];

console.log(check(arr, 0, 0));
console.log(check(arr, 0, 0, 0, 0));

1 Comment

Thank you for your thoughtful response. I selected another answer because it was more succinct.
0

For anyone finding this question after the introduction of optional chaining (circa 2020) this is now easily accomplished:

if (!arr[a]?.[b]?.[c]) {
  // the element is not present. Do something.
}

Here's a demo:

let a = [];
a[5] = [[1,2,3],[4,5,6],[7,8,9]];
a[6] = [undefined, undefined, undefined];
// works for empty items
console.log(a[0]?.[2]?.[1]); // prints `undefined`, a[0] is not set
console.log(a[5]?.[2]?.[1]); // prints `8`
// works for undefined items
console.log(a[6]?.[1]?.[1]); // prints `undefined`. a[6][1] is undefined

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.