如何从Javascript中的对象中删除所有的getter和setter,并保持纯值 [英] How to remove all getters and setters from an object in Javascript and keep pure value
本文介绍了如何从Javascript中的对象中删除所有的getter和setter,并保持纯值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何删除所有的getter和一个对象的setter,同时保留属性的值,所以我有一个简单的JavaScript对象?
解决方案
特殊情况/环境/设置可能看起来像...
var
obj = JSON.parse(JSON。字符串化(模型));
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);
另请参见... http://jsfiddle.net/w23uLttu/8/
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 library does not know how to handle.
How can I remove all getters and setters from an object while retaining the values of the properties, so I have a Plain Old JavaScript Object?
解决方案
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/
这篇关于如何从Javascript中的对象中删除所有的getter和setter,并保持纯值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文