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

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

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 Report

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 Report

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