Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!