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

117
Visualizações
Filter an object by value from array

I have an array of object b and an array a. I would like to filter b to only have objects where the to values are in a. I've tried many ways but I always receive an empty array. For example this case, the final result would be an array with the first and the second object from the array but not the last one.

const a = [
    "dfd302f4-571b-42da-9b35-07d1d9b6e68d",
    "d7099abd-6872-48db-bdd7-ca99e4513586",
    "93272093-5089-40cd-8c3d-2412377e1f32"
  ];

  const b = [
    {
      name: "toto",
      to: [
        "d7099abd-6872-48db-bdd7-ca99e4513586",
        "93272093-5089-40cd-8c3d-2412377e1f32"
      ]
    },
    { name: "tota", to: ["dfd302f4-571b-42da-9b35-07d1d9b6e68d"] },
    { name: "tota", to: ["8418v4rfe484evf48ez64vrfe6zv4r8ezvf4"] }
  ];

  console.log(b.filter(user => a.includes(user.to)))
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Need to check array with array (use some and includes)

const a = [
  "dfd302f4-571b-42da-9b35-07d1d9b6e68d",
  "d7099abd-6872-48db-bdd7-ca99e4513586",
  "93272093-5089-40cd-8c3d-2412377e1f32",
];

const b = [
  {
    name: "toto",
    to: [
      "d7099abd-6872-48db-bdd7-ca99e4513586",
      "93272093-5089-40cd-8c3d-2412377e1f32",
    ],
  },
  { name: "tota", to: ["dfd302f4-571b-42da-9b35-07d1d9b6e68d"] },
  { name: "tota", to: ["8418v4rfe484evf48ez64vrfe6zv4r8ezvf4"] },
];

console.log(b.filter(({ to }) => to.some((item) => a.includes(item))));

about 4 years ago · Juan Pablo Isaza Relatório

0

Your attempt,

console.log(b.filter(user => a.includes(user.to)))

fails to work because user.to is an array, and a does not include any arrays.

You need to use the every method to check if all elements of the array in the to property are inside a:

const a = [
    "dfd302f4-571b-42da-9b35-07d1d9b6e68d",
    "d7099abd-6872-48db-bdd7-ca99e4513586",
    "93272093-5089-40cd-8c3d-2412377e1f32"
  ];

const b = [
  {
    name: "toto",
    to: [
      "d7099abd-6872-48db-bdd7-ca99e4513586",
      "93272093-5089-40cd-8c3d-2412377e1f32"
    ]
  },
  { name: "tota", to: ["dfd302f4-571b-42da-9b35-07d1d9b6e68d"] },
  { name: "tota", to: ["8418v4rfe484evf48ez64vrfe6zv4r8ezvf4"] }
];

 console.log(b.filter(user => user.to.every(str => a.includes(str))));

EDIT: you could use some instead of every, as in @SivaKV's answer. That depends on whether you want an item to be included whose "to" array has some items in a and some not ("every" would not include these, but "some" would), which isn't clear from your question.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use .some or .every depending on whether it is sufficient that one or all elements of the .to property must be in array a.

const a = [
"dfd302f4-571b-42da-9b35-07d1d9b6e68d",
"d7099abd-6872-48db-bdd7-ca99e4513586",
"93272093-5089-40cd-8c3d-2412377e1f32"
  ];
const b = [
{
  name: "toto",
  to: [
    "d7099abd-6872-48db-bdd7-ca99e4513586",
    "93272093-5089-40cd-8c3d-2412377e1f32"
  ]
},
{ name: "tota", to: ["dfd302f4-571b-42da-9b35-07d1d9b6e68d"] },
{ name: "tota", to: ["8418v4rfe484evf48ez64vrfe6zv4r8ezvf4"] }
  ];

  console.log(b.filter(u => u.to.every(e=>a.includes(e))))

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