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

135
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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