Advertisement
xDisfigure

Angular Search In Object with dot string

Aug 17th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // To add in a HelperService :)
  2.     static recursiveValue(value:string, obj:any, translate:boolean = false) {
  3.         let splitted;
  4.         if(value.indexOf('.')) {
  5.             splitted = value.split('.');
  6.         } else {
  7.             splitted = [value];
  8.         }
  9.  
  10.  
  11.         if(angular.isDefined(obj[splitted[0]])) {
  12.             if(angular.isArray(obj[splitted[0]]) || angular.isObject(obj[splitted[0]])) {
  13.                 let splittedFirst = angular.copy(splitted[0]);
  14.                 return this.recursiveValue(this.splittedToStr(splitted, 0), obj[splittedFirst]);
  15.             } else {
  16.                 return obj[splitted[0]];
  17.             }
  18.         }
  19.     }
  20.  
  21.     static splittedToStr(splitted, i = 0) {
  22.         if(splitted.length === 1) {
  23.             return splitted[0];
  24.         }
  25.         splitted = splitted.slice(1, i);
  26.         return splitted.join('.');
  27.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement