Background
I have an array of objects (Users) defined and set as follows:
// object definition
function Users()
{
this.Id = null;
this.Name = null;
this.FirstName = null;
this.LastName = null;
this.IsActive = null;
this.Title = null;
this.index = null;
this.selected = null;
}
// object array
var AllUsers = [];
// ...
// an ajax call retrieves the Users and adds them to the AllUsers array
// ...
The index value is set on each User to the order in which they were retrieved. Once the Users are retrieved, they can then be selected, one-by-one, and moved from a list to a table on the page. Upon select, the selected property is set to true, for the selected object in the array.
I'm using grep to return all of the selected Users.
var SelectedUsers = $.grep(AllUsers,function(obj){
return obj["selected"] == true;
});
Here's an example of the data returned:
[
Object {
Id="00540000001AbCdEFG",
Name="First Last1",
FirstName="First",
LastName="Last1",
Title="Title",
index=56,
selected=true
},
Object {
Id="00540000001AbChIJK",
Name="First Last2",
FirstName="First",
LastName="Last2",
Title="Title",
index=12,
selected=true
},
Object {
Id="00540000001AbClMNO",
Name="First Last3",
FirstName="First",
LastName="Last3",
Title="Title",
index=92,
selected=true
}
]
Question
I want to be able to page through the data, and to do that, I need to be able to get the next and previous selected User by index. How can I do that?
If, for example, I open the first Selected User (index=56), in the table, how can I get the User with the next index (the third Selected User with index=92)?
sort()function is enough.$.grepis a new one to me.