Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda