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

87
Visualizações
how to filter based on two values from the array of array

I have a bunch array of objects with participants and messages, I want to filter the object with the below array of two participants, can anyone help with this

this is the example participnats of array :

["61badffe4ccf060b329441e0","61bc33a84ccf060b32944354"]

bunch of sample messages and participants :

  {
    "messages": [],
    "participants": [
      "61badffe4ccf060b329441e0",
      "61bae01b4ccf060b329441ef"
    ],
    "_id": "61bae0394ccf060b329441fb",
  },
  {
    "messages": [],
    "participants": [
      "61bae1014ccf060b3294420e",
      "61bae01b4ccf060b329441ef"
    ],
    "_id": "61bb230c4ccf060b3294421c"
  },
  {
    "messages": [],
    "participants": [
      "61badffe4ccf060b329441e0",
      "61bc33a84ccf060b32944354"
    ],
    "_id": "61d476dff651471663a72971",
  },
  {
    "messages": [],
    "participants": [
      "61badffe4ccf060b329441e0",
      "61e54b82eab592e7fef65656"
    ],
    "_id": "61e54ba3eab592e7fef6567a",
]





**expected below object after filter participants:** 

```{
    "messages": [],
    "participants": [
      "61badffe4ccf060b329441e0",
      "61bc33a84ccf060b32944354"
    ],
    "_id": "61d476dff651471663a72971",
  },
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You might wanna use Array.filter() and also in your case you should check every element from that array which should match your criteria.

const input = [{
    "messages": [],
    "participants": [
      "61badffe4ccf060b329441e0",
      "61bae01b4ccf060b329441ef"
    ],
    "_id": "61bae0394ccf060b329441fb",
  },
  {
    "messages": [],
    "participants": [
      "61bae1014ccf060b3294420e",
      "61bae01b4ccf060b329441ef"
    ],
    "_id": "61bb230c4ccf060b3294421c"
  },
  {
    "messages": [],
    "participants": [
      "61badffe4ccf060b329441e0",
      "61bc33a84ccf060b32944354"
    ],
    "_id": "61d476dff651471663a72971",
  },
  {
    "messages": [],
    "participants": [
      "61badffe4ccf060b329441e0",
      "61e54b82eab592e7fef65656"
    ],
    "_id": "61e54ba3eab592e7fef6567a",
    }
];

const filterByParticipants = ["61badffe4ccf060b329441e0","61bc33a84ccf060b32944354"];
const output = input.filter(el => el.participants.every(el => filterByParticipants.includes(el)));

console.log(output);

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to use filter() method of Array Object, to filter it based on certain conditions.
You can then use every() method of participants array, to check if every participant is present in the object.

let obj=[{messages:[],participants:["61badffe4ccf060b329441e0","61bae01b4ccf060b329441ef"],_id:"61bae0394ccf060b329441fb"},{messages:[],participants:["61bae1014ccf060b3294420e","61bae01b4ccf060b329441ef"],_id:"61bb230c4ccf060b3294421c"},{messages:[],participants:["61badffe4ccf060b329441e0","61bc33a84ccf060b32944354"],_id:"61d476dff651471663a72971"},{messages:[],participants:["61badffe4ccf060b329441e0","61e54b82eab592e7fef65656"],_id:"61e54ba3eab592e7fef6567a"}];

let participants =["61badffe4ccf060b329441e0","61bc33a84ccf060b32944354"];

let res = obj.filter((o)=>{
  let oParticipants = o.participants;
  let out = participants.every(p => oParticipants.includes(p))
  return out;
});

console.log(res)

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