json - Using filter with each on lodash -
i have json string
[ { uri : '/someuri/one', title : 'title 1', displaylocation : 'action_menu', masterdata : 'location', iconclass : 'icon-class-1' }, { uri : '/someuri/two', title : 'title 2', displaylocation : 'action_menu', masterdata : 'location', iconclass : 'icon-class-2' }, { uri : '/someuri/three', title : 'title 3', displaylocation : 'action_menu', masterdata : 'job', iconclass : 'icon-class-3' }, { uri : '/someuri/four', title : 'title 4', displaylocation : 'summary', masterdata : 'location', iconclass : 'icon-class-4' } ]
i converting to
[ { iconclass : 'icon-class-1', id : 'anythingunique', text : 'title 1' }, { iconclass : 'icon-class-2', id : 'anythingunique', text : 'title 2' } ]
using following code
function mycustomfilter(inputjsonstr) { return _.each(inputjsonstr.filter(function(action){ return action.masterdata === 'location' && action.displaylocation === 'action_menu'; }), function (action) { return [{iconclass: action.iconclass, id: 'anythingunique', text: action.title}]; });
but returning me json string
[ { uri : '/someuri/one', title : 'title 1', displaylocation : 'action_menu', masterdata : 'location', iconclass : 'icon-class-1' }, { uri : '/someuri/two', title : 'title 2', displaylocation : 'action_menu', masterdata : 'location', iconclass : 'icon-class-2' } ]
can suggest doing wrong?
you use map this:
_(inputjsonstr).filter({masterdata: 'location', displaylocation: 'action_menu'}) .map(function(a) { return {iconclass: a.iconclass, id: 'anythingunique', text: a.title}; }).value();
i've changed filter little, way if wanted, , i've used functional approach chaining, imperatively if makes more comfortable. map replaces array element returned element.
Comments
Post a Comment