I have a Angular utility server and in that I have one method which I am using for searching some values in a JSON as below.
angular.module('myAppModule')
.service('myService', ['$rootScope', function($rootScope)
{
this.myTestJson = '[{"id":1,"somthing":"somthing"},{"id":2,"somthing":"somthing"}]';
this.getJsonObj = function(id)
{
if(id == null || id == undefined || id == "")
return null;
// Units Convert to JSON
var jsonObj = JSON.parse(JSON.stringify(this.myTestJson)); // How can I avoid this doing every time ?
console.log("getJsonObj: jsonObj.length: "+jsonObj.length); //--> Printed invalid number, length of the string
for(var index=0;index < jsonObj.length;index++)
{
if( id == jsonObj[index].id )
return jsonObj[index];
}
return null;
};
}]);
Problem is I get JSON array length as invalid, and because of that I am not able to loop the array.
How can I access this JSON variable which is defined in the same service.