简体   繁体   English

循环遍历对象数组以查找具有匹配属性的对象

[英]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.

相关问题 遍历对象数组,检查匹配的参数并将匹配的对象添加到新数组 - Loop through array of objects, check for a matching parameter and add the matching object to new array 遍历数组以查找具有匹配ID的所有对象,然后将这些对象合并为数组中的一个元素。 - Loop through array to find all objects with matching ids, and merge those objects into one element in the array. 我有一个对象数组和一个对象,我想遍历对象,同时将对象值与数组中的值匹配 - I have an array of objects and an object I want to loop through an object while matching object values with the values in the array 使用下划线在具有对象的数组中查找匹配属性 - Using underscore to find matching property in a array with objects Javascript在对象数组中查找具有匹配属性的对象,如果存在则获取另一个属性 - Javascript find object with matching property in array of objects and get another property if exists JavaScript——在对象数组中找到匹配的对象 - JavaScript -- find matching object in array of objects 从对象数组中查找匹配的对象键 - Find matching object keys from an array of objects 在对象数组中查找最后匹配的 object - Find last matching object in array of objects JavaScript在对象数组中找到匹配的对象 - JavaScript find matching object in an array of objects 如何循环对象的javascript object并找到一个属性 - How to loop javascript object of objects and find a property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM