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

324
Visualizações
Iterate through map and remove entry whose value is empty (using Javascript)

I have below map object as follows

let monthMap = [
    {
        "key": "Oct 2021",
        "value": []
    },    
    {
        "key": "Dec 2021",
        "value": [
            "2021-12-06T08:00:00.000Z",
            "2021-12-13T08:00:00.000Z",
            "2021-12-20T08:00:00.000Z",
        ]
    },
    {
        "key": "Jan 2022",
        "value": [
            "2022-01-03T08:00:00.000Z",
            "2022-01-10T08:00:00.000Z",
        ]
    },
    {
        "key": "Feb 2022",
        "value": [
            "2022-02-07T08:00:00.000Z",
        ]
    }
]

I want to remove the key value pair from this map object whose value is empty array. for e.g. key Oct 2021 has empty value. so i want to eliminate that entry from the map object.

Can someone please let me know how to iterate through it and remove the empty value data from it.

I havent iterated through a map object and i tried few ways online but i wasnt able to iterate through it.

Ways i tried:

for (const [key, value] of monthMap.entries()) {
  console.log(key, value);
}

for (const [key, value] of Object.entries(monthMap)) {
  console.log(key, value);
}

Can someone please let me know how to iterate and delete entries dynamically.

Not sure if i am using correct structure of monthMap but this is how it shows in console log

enter image description here

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

use filter method of array

filteredMonthMap = monthMap.filter((month)=>{month.value && month.value.length !== 0;})

the condition inside of filter method means that value have some truthy value and its length not 0. be careful about value property data type it must be array at all.

about 4 years ago · Santiago Trujillo Relatório

0

You could use filter to achieve the same. Check for the value arrays length inside the callback.

More info on filter : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Edit:

If the input is a map , then you ought to convert map into key value pairs, filter the result and then convert it back to a map

let monthArr = [
  {
    key: "Oct 2021",
    value: []
  },
  {
    key: "Dec 2021",
    value: [
     "2021-12-06T08:00:00.000Z",
     "2021-12-13T08:00:00.000Z",
     "2021-12-20T08:00:00.000Z"
    ]
  },
  {
    key: "Jan 2022",
    value: ["2022-01-03T08:00:00.000Z", "2022-01-10T08:00:00.000Z"]
  },
  {
   key: "Feb 2022",
   value: ["2022-02-07T08:00:00.000Z"]
  }
];

const result = monthArr.filter((item) => {
  const { value } = item;
  if (value.length) return item;
});

console.info(`Month as an array`, result);

const inputMap = new Map([
  ["Oct 2021", []],
  [
    "Dec 2021",
    [
      "2021-12-06T08:00:00.000Z",
      "2021-12-13T08:00:00.000Z",
      "2021-12-20T08:00:00.000Z"
    ]
  ],
  ["Jan 2022", ["2022-01-03T08:00:00.000Z", "2022-01-10T08:00:00.000Z"]],
  ["Feb 2022", ["2022-02-07T08:00:00.000Z"]]
]);

const map1 = new Map([...inputMap].filter(([k, v]) => v.length));

console.info(`Month as a map`, [...map1]);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Santiago Trujillo Relatório

0

You can do it using Array.prototype.filter.

const
  monthMap = [{key:"Oct 2021",value:[]},{key:"Dec 2021",value:["2021-12-06T08:00:00.000Z","2021-12-13T08:00:00.000Z","2021-12-20T08:00:00.000Z",]},{key:"Jan 2022",value:["2022-01-03T08:00:00.000Z","2022-01-10T08:00:00.000Z",]},{key:"Feb 2022",value:["2022-02-07T08:00:00.000Z"]}],
  result = monthMap.filter((m) => m.value?.length);

console.log(result);

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