简体   繁体   English

使用 javascript 和 lodash 访问被提取的对象

[英]Get accessed to plucked object using javascript and lodash

I have an array of users in my javascript.我的 javascript 中有一组用户。 For each user I am comparing if that user appears in another list.对于每个用户,我正在比较该用户是否出现在另一个列表中。

var users = USERS.getUsers();

for (var i = 0; i < users.length; i++) {
    var u = users[i];
    if (util.pluck(myList.adminUsers, 'email').includes(u.email)) {
        // How do I check the value of the plucked value for myList.adminUsers.locationId ? 
        u.status = "admin";
    } 
}

users is an array of user objects. users 是一组用户对象。

[{...}]
    0:
        id: "1"
        email: "johndoe@gmail.com"
        firstname: "John"
        lastname: "Doe"
        roleid: "1" 
        _proto__: Object
    1:
        id: "2"
        email: "janedoe@gmail.com"
        firstname: "Jane"
        lastname: "Doe"
        roleid: "1"
        _proto__: Object

myList.adminUsers is also an array of user objects but also with locationId . myList.adminUsers也是一个用户对象数组,但也有locationId

[{...}]
    0:
        id: "1"
        email: "johndoe@gmail.com"
        firstname: "John"
        lastname: "Doe"
        roleid: "1"
        locationId: "123"
        _proto__: Object

I need to compare another field in this check.我需要比较此检查中的另一个字段。 So I need to see if the plucked object from myList.adminUsers has a field locationId that equals x , but I'm not sure how to do this?所以我需要看看从myList.adminUsers对象myList.adminUsers有一个等于x的字段 locationId ,但我不知道如何做到这一点?

How can I get access to the plucked object so I can check the value of locationId ?我怎样才能访问被提取的对象,以便我可以检查locationId的值?

Use findWhere instead of pluck .使用findWhere而不是pluck You can give it an object containing all the properties you want to match.你可以给它一个包含你想要匹配的所有属性的对象。

if (util.findWhere(myList.adminUsers, {email: u.email, locationId: u.locationId})) {
    u.status = "admin";
}

This should give you the list of admins that are found in the list of users and are in the location ID you provide as a value (I set it as a variable).这应该为您提供在用户列表中找到的管理员列表,并且在您作为值提供的位置 ID 中(我将其设置为变量)。

 const users = [ { id: "1", email: "johndoe@gmail.com", firstname: "John", lastname: "Doe", roleid: "1" }, { id: "2", email: "janedoe@gmail.com", firstname: "Jane", lastname: "Doe", roleid: "1" } ]; const admins = [ { id: "1", email: "johndoe@gmail.com", firstname: "John", lastname: "Doe", roleid: "1", locationId: "123" } ]; const locationIdToFind = '123'; const adminsWithLocation = _.intersectionWith(admins, users, (admin, user) => _.isEqual(admin.email, user.email) && _.isEqual(admin.locationId, locationIdToFind)); console.log(adminsWithLocation);
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM