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
Add a ment  | 

4 Answers 4

Reset to default 4

Plain 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