Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
JavaScript ES6+ compare fields in arrays of objects

Spinning my wheels here. Seems simple conceptually but JS is trying to kill me.

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. 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. In fact, frustratingly, I know I've done this before, but I just can't seem to make it happen today.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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).


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

about 4 years ago · Juan Pablo Isaza Relatório

0

Try some thing like below. Process one of array values to Set and use it another array every method.

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)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda