javascript - checking individual element of an array by calling a method -
can optimize code bit more reduce number of line?
i checking/passing individual array elements? can make re-write in generic way?
for (var = 0; < $scope.studentreport.students.length; i++) { if (_isvaluenan($$scope.studentreport.students[i].age)) $$scope.studentreport.students[i].age = null; if (_isvaluenan($$scope.studentreport.students[i].number)) $$scope.studentreport.students[i].number = null; if (_isvaluenan($$scope.studentreport.students[i].height)) $$scope.studentreport.students[i].height = null; } var _isvaluenan = function (item) { var result = false; if (typeof item == 'number' && isnan(item)) result = true; return result; }
you can nullify property internally in function, , pass item , property value in independently. example:
for (var = 0; < $scope.studentreport.students.length; i++) { _checkvaluenan($$scope.studentreport.students[i], "age"); _checkvaluenan($$scope.studentreport.students[i], "number"); _checkvaluenan($$scope.studentreport.students[i], "height"); } var _checkvaluenan = function (item, valuename) { if (typeof item[valuename] == 'number' && isnan(item[valuename])) item[valuename] = null; }
edit: leading on vicky gonsalves answer, additionally check of properties of object, might more scalable solution.
var _checkallvaluenan = function (item) { for(var key in item) { // iterates item properties if (!item.hasownproperty(key)) continue; // ensures not prop of prototype if (typeof item[key] == 'number' && isnan(item[key])) item[key] = null; } } (var = 0; < $scope.studentreport.students.length; i++) { _checkallvaluenan($scope.studentreport.students[i]); }
Comments
Post a Comment