Home > Mobile >  how to destructively mutate the Object in a function
how to destructively mutate the Object in a function

Time:06-17

I am trying to get the function destructivelyDeleteEmployeeByKey(employee, key) to return employee without the deleted key/value pair that would modify the original employee

this is what itsays in the test.js file

describe('destructivelyDeleteFromEmployeeByKey(employee, key)', function () {
it('returns employee without the deleted key/value pair', function () {
  let newEmployee = destructivelyDeleteFromEmployeeByKey(employee, 'name');

  expect(newEmployee['name']).to.equal(undefined);
});

it('modifies the original employee', function () {
  let newEmployee = destructivelyDeleteFromEmployeeByKey(employee, 'name');

  expect(employee['name']).to.equal(undefined);
  expect(employee).to.equal(newEmployee);
});

});

how do I write this function?

CodePudding user response:

you should use the delete opetator:

var person = {
  firstName:"John",
  lastName:"Doe",
  age:50,
  eyeColor:"blue"
};

delete person.age;  
OR
delete person["age"];

in you case you can delete based on the parameter you are passing

CodePudding user response:

Did you meeam something like that? I think it should work

const destructivelyDeleteFromEmployeeByKey = (employee, key) => {
   delete employee[key]
   return employee
}
  • Related