Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get next object in an array by property value?

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)?

JSFiddle

like image 201
Matt K Avatar asked Jun 20 '12 16:06

Matt K


2 Answers

Fiddle: http://jsfiddle.net/iambriansreed/KEXwM/

JavaScript Added:

SelectedUsers.sort(function(a,b){
      return a.index == b.index ? 0 : (a.index < b.index ? -1 : 1)});

If you didn't want to modify the original SelectedUsers array then define the sort to a new variable:

var SortedSelectedUsers = SelectedUsers.slice(0);
SortedSelectedUsers.sort(function(a,b){
      return a.index == b.index ? 0 : (a.index < b.index ? -1 : 1)});
like image 136
iambriansreed Avatar answered Sep 20 '22 01:09

iambriansreed


Why not just sort your array by index?

allUsers.sort(function(a, b) {
    return(a.index - b.index);
});

Then, the next selected user would be the next user in the array that was selected. You could just walk up the array until you found a selected user.

Or, you could use your grep function to get all the selected users and then sort that by index so the next selected user would just be the next one in the selectedUsers array like this:

var SelectedUsers = j$.grep(AllUsers,function(obj){
  return obj["selected"] == true;
}).sort(function(a, b) {
    return(a.index - b.index);
});
like image 39
jfriend00 Avatar answered Sep 22 '22 01:09

jfriend00