'How to remove functions from object javascript
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', 'companyDescription' ...];
var item = _.pick( vm.form, valuesToPick );
Solution 1:[1]
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];
}
Solution 2:[2]
Plain Javascript
var objectFunctionLess=JSON.parse(JSON.stringify(objectWithFunctions))
Solution 3:[3]
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 () {})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | jdh8 |
| Solution 2 | pdorgambide |
| Solution 3 | Nick Messing |
