jQuery Data Type How to - Get Amount of same strings in two arrays








Question

We would like to know how to get Amount of same strings in two arrays.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
var arr1 = ["how", "are", "you"],
    arr2 = ["how", "is", "it", "going"],
    count = 0;<!-- w w  w.  j  a v  a  2s .c  o m-->
    var count = 0;
    arr1.forEach(function (elem) {
        if (arr2.indexOf(elem) !== -1) {
            count++;
        }
    });
    document.writeln(count);
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: