Home > Software engineering >  Iterate through map and remove entry whose value is empty (using Javascript)
Iterate through map and remove entry whose value is empty (using Javascript)

Time:07-23

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

CodePudding user response:

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

CodePudding user response:

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.

CodePudding user response:

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

  • Related