Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript .startWith() function not working in IE, inside angularjs project

Hi im using Angularjs for my project, There is nationality search drop down. I want to map which is typing on Input and filter it inside nationality JSON object. This part is working fine in other browsers except IE. There is console error "Object doesn't support property or method 'startsWith'". this is my code, Can i know how to add "String.prototype.startsWith" for this issue for my code.

$scope.searchNationality = function (data) {
        var output = [];
        if (data != "" && data != undefined) {
            $scope.ShowNationalityDropDown = true;

            for (var i = 0; i < $scope.nationalityList.length; i++) {
                if ($scope.nationalityList[i].content.toLowerCase().startsWith(data.toLowerCase())) {
                    output.push($scope.nationalityList[i]);
                }
            }
            $scope.nationalityListSearchResults = output;
        } else {
            $scope.ShowNationalityDropDown = false;
            $scope.nationalityListSearchResults = [];
        }
    };
like image 718
Mahesh Sanjeewa Avatar asked Jun 21 '26 16:06

Mahesh Sanjeewa


2 Answers

You can try changing from .startsWith to .indexOf since it is compatible with IE for lower versions. If .indexOf returns 0 then the string is in the first position of the string that calls that function, which can be usable when you are in this kind of situation that you can't use .startsWith().

const str = "Hey this is a sample string!"
console.log(str.indexOf("Hey") === 0)
console.log(str.indexOf("sample") === 0)
like image 188
holydragon Avatar answered Jun 24 '26 06:06

holydragon


$scope.searchNationality = function (data) {
    var thereIsData = data != "" && data != undefined;
    var output = thereIsData 
        ? $scope.nationalityList.filter(function (nationality) {
            return nationality.content.toLowerCase().indexOf(data.toLowerCase())) == 0;
            })
        : [];
    $scope.ShowNationalityDropDown = thereIsData;
}
like image 33
Chris Tapay Avatar answered Jun 24 '26 05:06

Chris Tapay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!