Sample JavaScript Code:
1 2 3 4 5 6 7 8 9 10 | <script type="text/JavaScript"> var arr = [5, 15, 110, 210, 550]; var index = arr.indexOf(210); if (index > -1) { arr.splice(index, 1); } </script> |
Explanation:
- Line 1 – We have initialized an array named arr with few static values.
- Line 2 – Now we use
indexOf() function to find the index number of given value in array. If given value found in array , it will return index number, else it will remove values less than 0. - Line 3 – First check if return index number is >=0, then only delete the value from that index from array using
splice() function.
Comments
Post a Comment