admin管理员组文章数量:1290927
In my app, I'm using angular.js.
I need to send (through PUT request) an object with 3 properties, like this:
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. 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.
Is there any way to get only a
, b
and c
? Maybe with lodash?
My Solution:
I used lodash like this:
var valuesToPick = ['title', 'languageCode', 'jobTitle', 'location','economySegment', 'panyDescription' ...];
var item = _.pick( vm.form, valuesToPick );
In my app, I'm using angular.js.
I need to send (through PUT request) an object with 3 properties, like this:
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. 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.
Is there any way to get only a
, b
and c
? Maybe with lodash?
My Solution:
I used lodash like this:
var valuesToPick = ['title', 'languageCode', 'jobTitle', 'location','economySegment', 'panyDescription' ...];
var item = _.pick( vm.form, valuesToPick );
Share
Improve this question
edited Jan 13, 2020 at 19:14
Baumannzone
asked Apr 19, 2016 at 18:19
BaumannzoneBaumannzone
7802 gold badges20 silver badges38 bronze badges
4
-
Looks like you are putting your data on a
$resource
or$http
object, have you tried just putting your data on a new object like:var item= {}; item.a = 1;
etc – Patrick Evans Commented Apr 19, 2016 at 18:28 - I have a lot of properties, this is just an example, it has more, 20 I believe and I don't want to go like this 20 times :D. – Baumannzone Commented Apr 19, 2016 at 18:29
- 1 I would add your solution as an actual answer then mark it correct for future SO users – Chris Commented Apr 20, 2016 at 1:17
- I already did. Here you have it. – Baumannzone Commented Apr 20, 2016 at 21:57
4 Answers
Reset to default 4Plain Javascript
var objectFunctionLess=JSON.parse(JSON.stringify(objectWithFunctions))
They are inherit from Object
, not owned by item
.
If you want to iterate over own properties, use a for
...in
loop.
for (property in item) {
item[property];
}
I used lodash _.pick
like this:
var valuesToPick = ['title', 'languageCode', 'jobTitle', 'location','economySegment', 'panyDescription' ...];
var item = _.pick( vm.form, valuesToPick );
Items is now free of functions, just your info.
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 () {})
本文标签: angularjsHow to remove functions from object javascriptStack Overflow
版权声明:本文标题:angularjs - How to remove functions from object javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741502278a2382115.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论