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

148
Visualizações
Filter an array of objects from a dynamic array

I have this fixed array:

x = [
   {value: 0, text: "hello world"}, 
   {value: 1, text: "how are you?"}, 
   {value: 2, text: "no problem"}
   {value: 3, text: "anything else?"}
   {value: 4, text: "other dummy text"}
]

and another dynamic array:

y = [2, 4]

I would like to filter the array "x" based on the values from array "y"

the expected result should be:

x = [
   {value: 2, text: "no problem"},
   {value: 4, text: "other dummy text"}
]

How could I do this?

Thanks

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

0

After you get the dynamic array value. You can run the code like this it filters the array for the dynamic array value

let x = [
   {value: 0, text: "hello world"}, 
   {value: 1, text: "how are you?"}, 
   {value: 2, text: "no problem"},
   {value: 3, text: "anything else?"},
   {value: 4, text: "other dummy text"}
];

const y=[2,4];


x = x.filter( data => (y.includes(data.value)));

console.log(x);

about 4 years ago · Juan Pablo Isaza Relatório

0

what you are looking for is to filter array x, and check if its values is included in array y

const x = [
  { value: 0, text: "hello world" },
  { value: 1, text: "how are you?" },
  { value: 2, text: "no problem" },
  { value: 3, text: "anything else?" },
  { value: 4, text: "other dummy text" }
];
const y = [2, 4];

const result = x.filter((item) => y.includes(item.value));

console.log(result);
about 4 years ago · Juan Pablo Isaza Relatório

0

There you go.

x = [
   {value: 0, text: "hello world"}, 
   {value: 1, text: "how are you?"}, 
   {value: 2, text: "no problem"},
   {value: 3, text: "anything else?"},
   {value: 4, text: "other dummy text"}
]
y = [2, 4]
z = []
for (var i = 0;i<x.length;i++){
    for (var j = 0;j<y.length;j++){
        if (x[i].value == y[j]){z.push(x[i])}
    }
}
console.log(z)

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