I created a util service for my angular application. It has two functions that are used very often.
For example, you have an object.
First get the value from the object recursively without throwing an undefined error.
{prop: {nestedProp1: {nestedProp2: somevalue}}}; get nestedProp2 2 without undefined checks.
The second array of filters based on
[{prop: {nestedProp1: {nestedProp2: somevalue1}}}, {prop: {nestedProp1: {nestedProp2: somevalue2}}}];
Find an object from an array using nestedProp2 = somevalue2
app.service('UtilService', function(httpService) { this.mapStringKeyVal = function(map, field) { var lastIdentifiedVal = null; var parentVal = map; field.split('.').forEach(function(val){ if(parentVal[val]){ lastIdentifiedVal = parentVal[val]; parentVal = parentVal[val]; } }); return lastIdentifiedVal; } this.arrayPropFilter = function(array, field,value) { var lastIdentifiedVal = null; var mapStringKeyVal = this.mapStringKeyVal; array.forEach(function(arrayItem){ var valueFound = mapStringKeyVal(arrayItem,field); if(!lastIdentifiedVal && valueFound && valueFound==value){ lastIdentifiedVal = arrayItem; } }); return lastIdentifiedVal; }});
To resolve the current issue. enter UtilService and call
UtilService.arrayPropFilter(purposeArray,'purpose','daily');
Or more advanced
UtilService.arrayPropFilter(purposeArray,'purpose.nestedProp1.nestedProp2','daily');
sanjay patel Aug 07 '16 at 7:38 2016-08-07 07:38
source share