Showcaselfloyd Showcaselfloyd - 1 month ago 4
Javascript Question

How do i find if an object contains an array in javascript?

I have a huge set of data which is basically an array of objects. I need to loop through all the objects to see if any contain any array themselves.

Thoughts ?

Answer

Try this:

for (var i = 0; i < arr.length; i++) {
    var obj = arr[i];

    for (var j in obj) {
        if (!obj.hasOwnProperty(j)) {
            continue;
        }

        if (obj[j] instanceof Array) {
            console.log(i + '.' + j + ' is array');
        }
    }
}
Comments