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

194
Visualizações
From an array, find the array that has the true values for the objects

Given array: const array = [{1: true},{2: false},{3: true},{}.....];

Filter the given array by only including objects with values of true.

Looking for the shortest solution.

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

0

const onlyTrue = array.filter((el, ind) => el[ind + 1] === true);

This will work only if indexes in array objects are ordered and starting from 1, as it is in your example.

about 4 years ago · Juan Pablo Isaza Relatório

0

Assumptions (based on what's in your example):

  • Each object in the array only has at least 1 property in it
  • The first property on each object is the one we care about
  • The property in each object is different every time
const array = [{1: true},{2: false},{3: true}];
const results = array.filter(item => Object.values(item)[0]);

If you want to avoid any false positives from truth-y values (see https://developer.mozilla.org/en-US/docs/Glossary/Truthy), then change the filter call to this instead:

const results = array.filter(item => Object.values(item)[0] === true);
about 4 years ago · Juan Pablo Isaza Relatório

0

const array = [{1: true},{2: false},{3: true}];
const array2 = [];
array.forEach(filterTrue);
function filterTrue(item){
    for(x in item){
        if(item[x]===true){
            array2.push(item);
        }
    }
}
console.log(array2);

Hope this helps you.
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