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>