Ways to remove a property from an object using a dynamically determined name?

Currently, I'm receiving a value from an input field and assigning it to the name of a property within an object. However, I'm running into an issue when trying to delete this property.

function addPr() {
  let propName = prop.value;
  employees = Object.create(Object.prototype, {
    [propName]: {
      value: val.value
    },
  });
  console.log(employees);
  del.addEventListener("click", () => {
    delete employees[propName];
    console.log(employees);
  });

}

Answer №1

By default, properties have the characteristics of not being writable, enumerable, or configurable, however, if a property is set to be non-configurable, it becomes undeletable.

To make a property configurable, simply add configurable: true:

  staff = Object.create(Object.prototype, {
    [key]: {
      value: val.value,
      configurable: true
    }
  });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Upgrade to the most recent versions of packages using npm

When using npm install --save <package_name>, it typically installs the latest stable version of the package. If you want to specifically install the most recent release, such as Bootstrap v4, you would need to use npm install <a href="/cdn-cgi/l ...

Increase the date and time by a specified number of days

After searching for solutions on how to add a specific number of days to a given date, I came across various methods. However, my requirement is to add days to both Date and Time in the format MM-DD-YYYY HH:MM:SS. I attempted a method which worked fine wh ...

Modify the JS/JQuery variable by utilizing the data entered into an HTML input field

I have implemented a javascript/ajax request on my advanced search page. <script> // window.setInterval(function() // { $(function () { var SearchTerm = '<?php echo $_POST['Search']; ...

Every time I attempt to navigate to the login page, I find myself stuck in an endless loop

This code snippet demonstrates the router.beforeEach function in action. It checks if the client is authenticated, and if not, it redirects them to the login page. router.beforeEach(async (to, from, next) => { if ( to.name !== 'signup' || to ...

Transfer the values of specific keys in an array to a separate array based on the values of another key within the same object that satisfy a particular condition

I am seeking a refined solution to create a function that retrieves the names of legal drivers from a given array of people: function getNamesOfLegalDrivers(people) { } const driverArray = [ { name: 'John', age: 14 }, { name: 'Joey&apos ...

What is the best way to create a gradual color change in each individual cell instead of changing all at once?

Looking for some help with a grid I'm working on. I've got the cells changing colors like I want them to, but they're all changing at once. What I really need is for them to light up in a specific order - Yellow, Green, Blue, White, Orange. ...

Tips for ensuring my jQuery textarea Focus event does not fire continuously for an endless number of times

In my project code, I have a basic JavaScript jQuery code snippet that is supposed to be called only once. However, whenever I click my mouse cursor into the textarea attached to the event, it alerts more than 100 times! projectTaskModal.cache.$cmtTextare ...

Is AJAX causing issues with my media uploader and color picker?

Currently, I have incorporated tabbed navigation within a WordPress admin page and it is functioning properly on its own (data can be saved). However, I am now looking to implement some AJAX functionality for toggling between pages. The issue arises when t ...

How to add a group of items to a nested document using an index?

I've been working on the code below to create a new subdocument within an existing subdocument. The structure is as follows: User -> (Many Comments) -> (Many Ratings). The rating object is a simple JavaScript object with the format; rating: { ...

Is there a way for me to make the login button redirect to the Dashboard?

I'm currently working on a piece of code where I need to implement some form of validation when the user clicks on the sign-in button. Specifically, I want to ensure that both the username and password fields are not left empty. If this condition is m ...

Applying a class to specific instances of selected text in Javascript, rather than just the first occurrence

I am trying to enhance a text using JavaScript by adding a class and then sending that string to PHP. However, I have encountered an issue where the code adds the class to the first occurrence of my selection instead of the actual selected text. My goal is ...

Tips for managing variables to display or hide in various components using Angular

In this example, there are 3 main components: The first component is A.component.ts: This is the parent component where an HTTP call is made to retrieve a response. const res = this.http.post("https://api.com/abcde", { test: true, }); res.subscribe((r ...

Error encountered during execution of Angular application, specifically TS2305 error?

Hello, I am currently running an angular application by using the 'ng serve' command. I encountered the following error: ERROR in src/app/accounts/account-form/account-form.component.ts(29,3): error TS2305: Module '"/home/prasanth/primecas ...

What could be causing my second ajax call to render the page unresponsive?

I am encountering an issue with my AJAX call. It works fine on the first attempt, but when I try to call it a second time, the page becomes unresponsive. I am not sure what is causing this issue. The code is located within the document ready function. The ...

Changing direction of arrow upon dropdown menu opening with JavaScript

I have developed a piece of code that enables users to click on a div in order to reveal a dropdown menu containing radio buttons. My goal is to make the arrows rotate 180 degrees when the dropdown menus are opened, and then return to their original positi ...

Troubleshooting AngularJS binding problem when using ngRepeat to handle Collapse and Expand Caret icon

I am experimenting with implementing collapsible and expandable panels within an ngRepeat loop. Here is my approach: <tbody ng-repeat="item in Items"> <tr data-toggle="collapse" class="accordion-toggle"> <td>{{item.name}}< ...

Issue with Angular's ngOnChanges Lifecycle Hook Preventing Function ExecutionWhen attempting to run a function within Angular's ngOn

In the midst of my evaluation process to ensure that specific values are properly transmitted from one component to another using Angular's custom Output() and EventEmitter(), I am encountering some issues. These values are being sent from the view of ...

Display or conceal nested divs within ng-repeat loop

I set up a div with sub divs to establish a nested grid system. There are three levels altogether: MainDiv - Always Visible SecondDiv - Display or conceal when MainDiv is clicked on ThirdDiv - Display or conceal when SecondDiv is clicked on <div c ...

What are some ways to maintain code efficiency when working with AJAX requests?

Looking at the code below, I am making two identical Ajax requests with only one line of difference. Is there a way to consolidate this into a function to maintain DRY (Don't Repeat Yourself) code? $('.searchable').multiSelect({ selecta ...

Blocking a thread on getJSON: Step by step guide

Currently facing a situation where I have to perform two web service calls in my JQuery 1.9.1 application. The outcome of the first call is required before proceeding with the second one. After attempting getJSON requests, it seems that they do not block ...