0

I have an array of typed objects that is passed into my view from a controller. The requirement is to check if a string value matches any of the AdminEmailAddress string type objects within that array.

What I have tried is creating an if condition to check if the string currentUserEmail matches any of the admin emails within that list, using indexOf.

But the script breaks on the if condition:

 if (adminUserList.AdminEmailAddress.indexOf(currentUserEmail) > -1)

I also put an alert on the currentUserEmail string and the adminUserList array which are both populated with data.

The contents of the adminUserList during runtime is as follows for reference:

var adminUserList = [{"AdminID":1,"AdminDisplayName":"brianb","AdminEmailAddress":"[email protected]"},{"AdminID":2,"AdminDisplayName":"wendy","AdminEmailAddress":"[email protected]"}];

Question:

How can you check if a string matches a property value within an array?

Code:

List type containing the AdminEmailAddress property -

List type public class AdminUsers
{
    public int AdminID { get; set; }
    public string AdminDisplayName { get; set; }
    public string AdminEmailAddress { get; set; }

}

List passed in from Controller to View:

List<AdminUsers> adminList = sqlConnection.GetAdminUsers();
ViewBag.AdminUserList = adminList;

Script that contains the if condition:

<script>

    var currentUserEmail = '@ViewBag.CurrUserEmail';
    var adminUserList = @Html.Raw(Json.Encode(ViewBag.AdminUserList));


    $(document).ready(function () {

        var historyTable = $('#table').DataTable({
            "order": [[6, "desc"]]
        });


        if (adminUserList.AdminEmailAddress.indexOf(currentUserEmail) > -1)
        {
            historyTable.columns([9]).visible(false);
        }



    });
</script>

3 Answers 3

1

Use following function:

function checkEmailExists(currentUserEmail){

        var match = false;
        for(var i=0;i<adminUserList.length;i++){
            if(adminUserList[i]["AdminEmailAddress"].indexOf(currentUserEmail) > -1){
                match = true;
                break;
            }

        }

        return match;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming the following situation:

var adminUserList = [{"AdminID":1,"AdminDisplayName":"brianb","AdminEmailAddress":"[email protected]"},{"AdminID":2,"AdminDisplayName":"wendy","AdminEmailAddress":"[email protected]"}];
var currentUserEmail = '[email protected]';

Then you can use filter to find elements:

var matchArray = adminUserList.filter(function(element){
  return element.AdminEmailAddress === currentUserEmail;
})
alert(matchArray.length > 0);

This way you also have access to the actual results:

if(matchArray.length > 0) {
    alert(matchArray[0].AdminDisplayName)
}

Comments

1

You could use the find function to find the an element of the array:

var adminUserList = [{"AdminID":1,"AdminDisplayName":"brianb","AdminEmailAddress":"[email protected]"},{"AdminID":2,"AdminDisplayName":"wendy","AdminEmailAddress":"[email protected]"}];

var mailToCheck = '[email protected]'

var adminFound =  adminUserList.find(function(admin){ return admin.AdminEmailAddres === mailToCheck});

If the user is found the user will be returned, otherwise the adminFound will be undefined.

But why do that in the front-end ? User data available in the browser, I do not think is a good practice. I suggest that you do that in your backend and return a true/false response.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.