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

129
Views
JS - Filtering nested array

is in JS any way to filter nested array values?

The desired result is to filter null values in nested array.

I tried the code below, but it is not correct.

const test = [
  [
    null,
    {
        "start_time": "2022-08-25T08:45:00.000Z",
        "end_time": "2022-08-25T09:45:00.000Z"
    },
    null,
    null,
    null,
    null,
    {
        "start_time": "2022-08-25T10:00:00.000Z",
        "end_time": "2022-08-25T11:00:00.000Z"
    },
  ]
]

let arr = test.filter(item => {
  return item != null;
})

console.log(arr);
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can use Array#map before Array#filter as follows:

const test = [
  [
    null,
    {
        "start_time": "2022-08-25T08:45:00.000Z",
        "end_time": "2022-08-25T09:45:00.000Z"
    },
    null,
    null,
    null,
    null,
    {
        "start_time": "2022-08-25T10:00:00.000Z",
        "end_time": "2022-08-25T11:00:00.000Z"
    },
  ]
]

const arr = test.map(
    items => items.filter(item => {
      return item !== null;
    })
);

console.log(arr);

about 4 years ago · Santiago Trujillo Report

0

Of course, you can reduce the amount of code

const test = [[null,{"start_time": "2022-08-25T08:45:00.000Z","end_time": "2022-08-25T09:45:00.000Z"},null,null,null,null,{"start_time": "2022-08-25T10:00:00.000Z","end_time": "2022-08-25T11:00:00.000Z"}]];

const arr = test.map(e => e.filter(Boolean));

console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: 0 }

about 4 years ago · Santiago Trujillo 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!