Javascript Data Type How to - Search item in two dimensional array








Question

We would like to know how to search item in two dimensional array.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!--from  w  w  w . j  a  v  a  2  s  . c o m-->
var array = [
    ['apple', 23, 1, 20],
    ['orange', 12, 10, 10]
];
for (var i = 0; i < array.length; i++) {
    if (array[i].indexOf(10) > -1) {
        document.writeln("Yep");
    } else {
        document.writeln("nope");
    }
}
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: