-8

How to delete a specific item from JSON using javascript

A mainly I want to delete Mon -> from time to be empty after clicking a button

const arr = {
    "Mon": [
        {
          "from": "08:00 AM",
          "to": "09:00 AM"
        },
        {
          "from": "06:00 PM",
          "to": "07:00 PM"
        }
      ],

    "Sun": [
        {
        "from": "",
        "to": ""
        },
        {
        "from": "",
        "to": ""
        }
    ]
};

const arr = {

    "Mon": [
        {
          "from": "08:00 AM",
          "to": "09:00 AM"
        },
        {
          "from": "06:00 PM",
          "to": "07:00 PM"
        }
      ],
      
    "Sun": [
        {
        "from": "",
        "to": ""
        },
        {
        "from": "",
        "to": ""
        }
    ]
};

it is not working please help me

Eddie
  • 26,593
  • 6
  • 36
  • 58

1 Answers1

0

I am a slight novice to Javascript but I think you are using const which should be var. Then you can completely remove object through delete as mentioned below:

 delete arr.Mon.from;

or to make it empty you can use

  arr.Mon.from = new Date(); 

or to make it null you can use

arr.Mon.from = null; 
Ullas Hunka
  • 2,119
  • 1
  • 15
  • 28