Unfortunately, I am struggling to come up with a specific title for what I am attempting to achieve. The issue at hand involves manipulating a JSON list within an angular framework. Below is an example of the structure:
$scope.users =
{
// The list name and "title" must match
Guest:
{
title: 'Guest',
list:
[
{ id: "0", name: "Stephen" },
{ id: "1", name: "Mitch"},
{ id: "2", name: "Nate"},
{ id: "3", name: "Rob" },
{ id: "4", name: "Capt. Jack"},
{ id: "5", name: "Herman" }
]
},
Admin:
{
title: 'Admin',
list:
[]
}
};
The challenge is to dynamically assess a string (for instance, "Guest," "Admin," or any other user-group yet to be created) in order to transfer a user from one user-group to another.
The function in question appears as follows:
$scope.moveUser = function(fromId, toId, index) {
scope.users.toId.list.push(scope.users.fromId.list[index]);
scope.users.fromId.list.splice(index, 1);
};
"fromId" and "toId" are strings that should represent the name of a user-group ("Admin" or "Guest"). Presently, the function attempts to locate a JSON field named "toId" and encounters errors if none exists. How can I first evaluate the string so that if "toId" equals "Guest" and "fromId" equals "Admin," my function alters to:
scope.users.Guest.list.push(scope.users.Admin.list[index]);
scope.users.Admin.list.splice(index, 1);