[英]Loop through array of objects to find object with matching property
I have 1 array, one with a list of all my users with unique IDs. 我有一个数组,一个包含所有具有唯一ID的用户的列表。 I have an object which contains contains a selected groups information. 我有一个包含所选组信息的对象。 Part of that information is the owners ID. 部分信息是所有者ID。 I'm trying to figure out, how do I get the users's information given the groups owner ID? 我想知道,如何根据群组所有者ID获取用户的信息? For example, the student group object has an owner ID of 70, there's a user on my sites who's ID is 70...how do I match them up? 例如,学生组对象的所有者ID为70,我的网站上有一个用户ID为70的用户...如何匹配它们?
users:
[ {
id: 68
name: mike
domain: i:0#.f|admembers|mike.ca
email: mike.ca
isAdmin: False
}, etc etc ]
selectedGroup: {
name: Students
id: 78
description:
owner: 70
ownerIsUser: True
}
You'll have to loop through users
: 你必须循环users
:
var i = users.length,
ownerData;
while(i--) {
if(selectedGroup.owner == users[i].id) {
ownerData = users[i];
break;
}
}
Or you could use Array.filter()
: 或者您可以使用Array.filter()
:
var ownerData = users.filter(function(user) {
return user.id === selectedGroup.owner;
})[0];
In ECMAScript 6, you could use the native Array.find
method : 在ECMAScript 6中,您可以使用本机Array.find
方法 :
var selectedUser = users.find( function( user ){
return user.id === 70;
} );
Seeing as only the latest Firefox supports this for the moment, you could use a library like underscore.js : 看到目前只有最新的Firefox支持这一点,你可以使用像underscore.js这样的库:
var selectedUser = _.find( users, function( user ){
return user.id === 70;
} );
…or you could use a wrapper around the slightly less recent forEach
method : ...或者您可以使用稍微不那么新的forEach
方法的包装器:
var selectedUser;
users.forEach( function( user ){
if( user.id === 70 ){
selectedUser = user;
}
} );
But if you want to use script that'll support legacy browsers without using libraries, you'll need a for loop: 但是如果你想使用不使用库而支持旧浏览器的脚本,你需要一个for循环:
var selectedUser;
for( var i = 0; i < users.length; i++ ){
if( users[ i ].id === 70 ){
selectedUser = users[ i ];
break;
}
};
Have a look at Underscore.js to trivialize this, like so: 看看Underscore.js来轻视这个,就像这样:
_.findWhere(users, { id: 68 })
Naturally you could pass in a variable to match like: 你自然可以传入一个变量来匹配:
_.findWhere(users, { id: selectedGroup.owner })
You could just loop over the array to match that: 您可以循环遍历数组以匹配:
var match = function (array, matchfn) {
var i;
for (i in array) {
if (matchfn(array[i])) {
return array[i];
}
}
};
var user = match(users, function (u) { return u.id == 70; });
If you have to work with existing javascript objects, I think a brute-force solution is the only option: 如果你必须使用现有的javascript对象,我认为蛮力解决方案是唯一的选择:
var owner;
for(var i = 0; i < users.length; i++) {
if (users[i].id == selectedGroup.id) {
owner = users[i];
break;
}
}
if (owner) {
console.log(owner);
}
Depending on how you use it, it may be more efficient to restructure your first object so you can directly access a property: 根据您的使用方式,重构第一个对象可能更有效,因此您可以直接访问属性:
var users = {
"68": {
id: 68,
name: 'mike',
domain: 'i: 0#.f | admembers | mike.ca',
email: 'mike.ca',
isAdmin: false
}
};
var selectedGroup = {
name: 'Students',
id: 78,
description: '',
owner: 68,
ownerIsUser: 'True'
};
var owner = users[selectedGroup.owner];
You can also use Array.prototype.some to compare all of the objects properties to see if they contain equal values. 您还可以使用Array.prototype.some来比较所有对象属性,以查看它们是否包含相等的值。
function haveSameValues(oneObject, anotherObject) {
var hasDifferentKeyValues = Object.keys(oneObject).some(function(key){
return oneObject[key] !== anotherObject[key]
});
return !hasDifferentKeyValues;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.