Javascript Data Type How to - Search Array and remove string








Question

We would like to know how to search Array and remove string.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!--from w  w w .j  av  a  2 s . co m-->
function remove(arr, what) {
    var found = arr.indexOf(what);
    while (found !== -1) {
        arr.splice(found, 1);
        found = arr.indexOf(what);
    }
}
var array = new Array();
array.push("A");
array.push("B");
array.push("C");
remove(array, 'B');
document.writeln(array);

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: