Skip to content Skip to sidebar Skip to footer

How To Remove All Getters And Setters From An Object In Javascript And Keep Pure Value

I'm using a library called which takes a JS object as an input. Unfortunately, the JSONP api that I am using returns an object containing getters and setters, which this particular

Solution 1:

a solution for this special case/environment/setting might look like that ...

var
  obj = JSON.parse(JSON.stringify(model));

console.log("obj before : ", obj);

Object.keys(obj).reduce(function (obj, key) {

  if (obj["_" +  key] === obj[key]) {
    delete obj["_" +  key];
  }
  return obj;

}, obj);

console.log("obj after : ", obj);

see also ... http://jsfiddle.net/w23uLttu/8/

Solution 2:

After trying all sorts of things such as serializing to JSON and back, I ended up writing a simple shallowClone function to handle this:

constshallowClone = (obj) => {
  returnObject.keys(obj).reduce((clone, key) => {
    clone[key] = obj[key];
    return clone;
  }, {});
}

Post a Comment for "How To Remove All Getters And Setters From An Object In Javascript And Keep Pure Value"