javascript - How to correctly use lodash pick method -


i'm attempting use underscore filter out properties of object. beginning of following code works expected, .pick not working. i'm aiming limit properties of returned object strings listed in .pick method.

var result = _.chain(data)   .each(function(item) {     item.answers = [];     _.each(data, function(object) {       if (item.id === object.id) {         item.answers.push({           id: object.answer_id,           email: object.answer_email,           date: object.answer_date         });       }     });     item = _.pick(item,       'id',       'owner_id',       'url',       'enabled',       'review_date',       'answers'     );   })   .uniq(function(item) {     return item.id;   })   .value(); 

the array start with, 'data', looks this:

[   {     id: '8ffdf27b-5a90-478a-b263-dhhdhdhhdhd',     answer_date: fri oct 30 2015 14:35:07 gmt-0400 (edt),     answer_id: 1,     answer_email: 'test@example.com',     owner_id: 5,     url: 'media/5-4a3640ac-ec13-fhhfh-ac0a-fhjhdhhdhd.jpg',     enabled: false,     review_date: sun nov 01 2015 13:57:32 gmt-0500 (est)   }, ... ] 

the returned array 'should' so:

[   {     id: '8ffdf27b-5a90-478a-b263-dhhdhdhhdhd',     owner_id: 5,     url: 'media/5-4a3640ac-ec13-fhhfh-ac0a-fhjhdhhdhd.jpg',     enabled: false,     review_date: sun nov 01 2015 13:57:32 gmt-0500 (est),     answers: [{...}, {...}]   }, ... ] 

but instead looks this:

[   {     id: '8ffdf27b-5a90-478a-b263-dhhdhdhhdhd',     answer_date: fri oct 30 2015 14:35:07 gmt-0400 (edt),     answer_id: 1,     answer_email: 'test@example.com',     owner_id: 5,     url: 'media/5-4a3640ac-ec13-fhhfh-ac0a-fhjhdhhdhd.jpg',     enabled: false,     review_date: sun nov 01 2015 13:57:32 gmt-0500 (est),     answers: [{...}, {...}]   }, ... ] 

you should use map() instead of each() change array (note have return modified item in map function):

var result = _.chain(data) .map(function (item) {   item.answers = [];   _.each(data, function (object) {     if (item.id === object.id) {       item.answers.push({         id: object.answer_id,         email: object.answer_email,         date: object.answer_date       });     }   });   item = _.pick(item,     'id',     'owner_id',     'url',     'enabled',     'review_date',     'answers'   );   return item; }) .uniq(function (item) {   return item.id; }) .value(); 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -