admin管理员组文章数量:1289403
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
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
Share Improve this question edited Jun 20, 2012 at 16:36 Matt K asked Jun 20, 2012 at 16:20 Matt KMatt K 7,3376 gold badges41 silver badges62 bronze badges 4- 3 +1 for nicely explained question as well as fiddle – Sarfraz Commented Jun 20, 2012 at 16:23
- 1 It seems like you will have to either sort or rearrange the array to fit your needs; if the array is small, it should efficient enough to iterate through all the elements to index them differently. – Mahn Commented Jun 20, 2012 at 16:29
-
@Mahn no need to do that,
sort()
function is enough. – Cranio Commented Jun 20, 2012 at 16:35 -
2
Thanks OP.
$.grep
is a new one to me. – iambriansreed Commented Jun 20, 2012 at 16:37
3 Answers
Reset to default 3Fiddle: http://jsfiddle/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)});
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);
});
Implement your own sorting function by index:
function pareUsers(a,b)
{
return (a.index - b.index);
}
And sort the objects:
SelectedUsers.sort(pareUsers);
Now you have all the indexes in the correct order. From this, it's easy to get the following or preceding object by index number.
本文标签: javascriptGet next object in an array by property valueStack Overflow
版权声明:本文标题:javascript - Get next object in an array by property value? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741400071a2376610.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论