[英]How to remove functions from object javascript
In my app, I'm using angular.js .在我的应用程序中,我使用的是angular.js 。
I need to send (through PUT request) an object with 3 properties, like this:我需要(通过 PUT 请求)发送具有 3 个属性的对象,如下所示:
var item = {
a: 1,
b: 2,
c: 3
}
But when I do console.log(item)
, it shows a lot of information I don't need, such as $delete
, $get
, and so on.但是当我执行
console.log(item)
时,它会显示很多我不需要的信息,例如$delete
、 $get
等。 Like this:像这样:
{
...
$delete: function (params, success, error),
$get: function (params, success, error),
...
a: 1,
b: 2,
c: 3
}
I think it's because is a javascript promise or something.我认为这是因为是 javascript 承诺之类的。
Is there any way to get only a
, b
and c
?有没有办法只得到
a
, b
和c
? Maybe with lodash?也许用 lodash?
My Solution:我的解决方案:
I used lodash like this:我像这样使用lodash :
var valuesToPick = ['title', 'languageCode', 'jobTitle', 'location','economySegment', 'companyDescription' ...];
var item = _.pick( vm.form, valuesToPick );
They are inherit from Object
, not owned by item
.它们继承自
Object
,不属于item
。
If you want to iterate over own properties, use a for
... in
loop.如果要迭代自己的属性,请使用
for
... in
循环。
for (property in item) {
item[property];
}
纯Javascript
var objectFunctionLess=JSON.parse(JSON.stringify(objectWithFunctions))
This is not a perfect solution but it should work:这不是一个完美的解决方案,但它应该可以工作:
Instead of:代替:
submitFunction(object, function () {})
do the:做:
submitFunction(JSON.parse(angular.toJson(object)), function () {})
you can split it into:您可以将其拆分为:
var cleanObject = JSON.parse(angular.toJson(object))
submitFunction(cleanObject, function () {})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.