简体   繁体   English

JavaScript ES6+ 比较对象数组中的字段

[英]JavaScript ES6+ compare fields in arrays of objects

Spinning my wheels here.在这里转动我的轮子。 Seems simple conceptually but JS is trying to kill me.概念上看起来很简单,但 JS 正试图杀死我。

Two arrays of objects:两个对象数组:

let allProfileUsers = [
    {
        id: "0b4cd920-31da-11ec-a31c-cd844bfb73be",
        auth_user_id: "fea98060-31ac-11ec-81f6-9b65b19a8154",
        active: true
    },
    {
        id: "0b4f9840-31da-11ec-a31c-cd844bfb73be",
        auth_user_id: "fea73670-31ac-11ec-81f6-9b65b19a8154",
        active: true
    },
    {
        id: "0b51e230-31da-11ec-a31c-cd844bfb73be",
        auth_user_id: "fea98060-31ac-11ec-81f6-9b65b19a8154",
        active: true
    }
];

let allAuthUsers = [
    {
        id: "fea4c570-31ac-11ec-81f6-9b65b19a8154",
        username: "user1",
        active: true,
    },
    {
        id: "fea73670-31ac-11ec-81f6-9b65b19a8154",
        username: "user2",
        active: true,
    },
    {
        id: "fea98060-31ac-11ec-81f6-9b65b19a8154",
        username: "user3",
        active: true,
    }
];

I need to compare the "id" field in the objects in the first array to the "auth_user_id" field in the second set of arrays.我需要将第一个数组中对象中的“id”字段与第二组数组中的“auth_user_id”字段进行比较。 Basically, all users in array one should exist in array two, based on the match-up of those two fields.基本上,基于这两个字段的匹配,数组一中的所有用户都应该存在于数组二中。

This doesn't work (not shorthand so it's easy to debug):这不起作用(不是速记,所以很容易调试):

let allMatched = allAuthUsers.every(x => {
    return allProfileUsers.some(pu => {
        return x.id === pu.auth_user_id;
    });
});

...returns false; ...返回假;

Of course, I could manually loop through each value and compare them.当然,我可以手动遍历每个值并进行比较。 I don't want that unless there's no other way.除非别无他法,否则我不想要。

I have to think JS is capable of a more elegant one-liner with arrows.我不得不认为 JS 能够制作出更优雅的带箭头的单线。 In fact, frustratingly, I know I've done this before, but I just can't seem to make it happen today.事实上,令人沮丧的是,我知道我以前做过这件事,但我今天似乎无法做到。

Buddy, I feel the answer is in your question.伙计,我觉得答案就在你的问题中。 You need to loop through the first array(allProfileUsers) first and then check if it exist in second(allAuthUsers).您需要先遍历第一个数组(allProfileUsers),然后检查它是否存在于第二个(allAuthUsers)中。


let allMatched = allProfileUsers.every(pu => {
    return allAuthUsers.some(x => {
            return x.id === pu.auth_user_id;
    });
});

Try some thing like below.尝试一些类似下面的事情。 Process one of array values to Set and use it another array every method.将数组值之一处理为Set并在every方法中使用另一个数组。

 let allProfileUsers = [ { id: "0b4cd920-31da-11ec-a31c-cd844bfb73be", auth_user_id: "fea98060-31ac-11ec-81f6-9b65b19a8154", active: true, }, { id: "0b4f9840-31da-11ec-a31c-cd844bfb73be", auth_user_id: "fea73670-31ac-11ec-81f6-9b65b19a8154", active: true, }, { id: "0b51e230-31da-11ec-a31c-cd844bfb73be", auth_user_id: "fea98060-31ac-11ec-81f6-9b65b19a8154", active: true, }, ]; let allAuthUsers = [ { id: "fea4c570-31ac-11ec-81f6-9b65b19a8154", username: "user1", active: true, }, { id: "fea73670-31ac-11ec-81f6-9b65b19a8154", username: "user2", active: true, }, { id: "fea98060-31ac-11ec-81f6-9b65b19a8154", username: "user3", active: true, }, ]; const auth = new Set(allAuthUsers.map(({ id }) => id)); const isExist = allProfileUsers.every(({ auth_user_id }) => auth.has(auth_user_id) ); console.log(isExist)

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

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