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

132
Views
Remove empty array from array of objects

I have an array of object arrays, and one of the dates is empty, I would like to remove that empty date.

const arr = [
  {
    "2022-03-10": [
      { "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Consulta General", "read": false }
    ],
    "2022-03-26": [
      { "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Follow Up", "read": false }
    ],
    "2022-03-27": [],
    "2022-03-31": [
      { "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Valoraciones Funcionales", "read": false }
    ]
  }
];

What I've tried

list.push(doc.data());
var filtered = list.filter(function (el) {
  return el != " ";
});

I would like to keep the same array, just without the "2022-03-27" item.

Also, if it is also possible to filter directly from the doc.data() without having to do the list.push, that would be much better.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

  • Using Array#map, iterate over the array
  • Using Object#entries, get the list of entries for the current object
  • Using Array#filter, filter the entries whose value is an empty array
  • Using Object#fromEntries, return the current object containing the filtered entries

const arr = [
  {
    "2022-03-10": [
      { "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Consulta General", "read": false }
    ],
    "2022-03-26": [
      { "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Follow Up", "read": false }
    ],
    "2022-03-27": [],
    "2022-03-31": [
      { "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Valoraciones Funcionales", "read": false }
    ]
  }
];

const filtered = arr.map(obj => 
  Object.fromEntries(
    Object.entries(obj).filter(([key, value]) => value.length > 0)
  )
);

console.log(filtered);

about 4 years ago · Juan Pablo Isaza Report

0

It looks like you need to review data structure. I restructured your code to conventional format I hope that what you mean to do. if not leave me a comment and I will recode it for you

const result = [
     { 
      "2022-03-10": [],
        "Age": "31",
        "Last": "Craig",
        "Name": "Carlos",
        "Time": "11:00am - 12:00pm",
        "Type": "Consulta General",
        "read": false,
      },
        {
        "2022-03-26": [],
        "Age": "31",
        "Last": "Craig",
        "Name": "Carlos",
        "Time": "11:00am - 12:00pm",
        "Type": "Follow Up",
        "read": false,
      },

      { "2022-03-27": [] },
      
      {
        "2022-03-31": [],
        "Age": "31",
        "Last": "Craig",
        "Name": "Carlos",
        "Time": "11:00am - 12:00pm",
        "Type": "Valoraciones Funcionales",
        "read": false,
      },
    ];
  
    const removeEmpty = result.filter(oneObj => Object.values(oneObj).length > 1);
  
    console.log(removeEmpty)

about 4 years ago · Juan Pablo Isaza Report

0

You can use delete to remove the parts of the object that have an empty array. This changes the original array.

const arr = [{
    "2022-03-10": [
      { "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Consulta General", "read": false }
    ],
    "2022-03-26": [
      { "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Follow Up", "read": false }
    ],
    "2022-03-27": [],
    "2022-03-31": [
      { "Age": "31", "Last": "Craig", "Name": "Carlos", "Time": "11:00am - 12:00pm", "Type": "Valoraciones Funcionales", "read": false }
    ]
}];

arr.forEach(e =>
    Object.entries(e).forEach(([key,value]) => value.length || delete e[key])
);

console.log( arr );

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!