Hi let's say that I have an array like this in javascript:
var arr = ["ftp_text_1", "abc_text_2", "ftp_text_3"];
How do I remove from all the strings from my array that start with ftp_
Thanks
To remove an object from an array by its value: Call the findIndex() method to get the index of the object in the array. Use the splice() method to remove the element at that index. The splice method changes the contents of the array by removing or replacing existing elements.
Approach 1: Store the index of array elements into another array which need to be removed. Start a loop and run it to the number of elements in the array. Use splice() method to remove the element at a particular index.
Simply use Array.filter:
arr = arr.filter(function (item) {
return item.indexOf("ftp_") !== 0;
});
Edit: for IE9- support you may use jQuery.grep:
arr = $.grep(arr, function (item) {
return item.indexOf("ftp_") !== 0;
});
Use a regular expression:
$.each(arr, function (index, value) {
if (value.match("^ftp_")) {
arr.splice(index, 1);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With