How to push JSON Object to array in AngularJs -


need push json object angular js , need check before if value 1 of object exist need overwrite data,

$scope.setdata = function(survey, choice) {    keepalldata.push({     'surveyid': survey.id,     'choiceid': choice.id   });   console.log(keepalldata);   toarray(keepalldata);   alert(json.stringify(toarray(keepalldata)));    $scope.keepalldatas.push({     'surveyid': survey.id,     'choiceid': choice.id   });   var items = ($filter('filter')(keepalldatas, {     surveyid: survey.id   }));  }  function toarray(obj) {   var result = [];   (var prop in obj) {     var value = obj[prop];     console.log(prop);     if (typeof value === 'object') {       result.push(toarray(value));        console.log(result);     } else {       result.push(value);       console.log(result);     }   }   return result; } 

if survey id exist in keepalldata need change recent value choiceid. please let me know possible angular js

try :before pushing data have check if survey id existing or not, if existing have update choice corresponding survey id ,otherwise can push directly.

$scope.setdata = function(survey, choice) {   var item = $filter('filter')(keepalldata, {     surveyid: survey.id   });   if (!item.length) {     keepalldata.push({       'surveyid': survey.id,       'choiceid': choice.id     });   } else {     item[0].choiceid = choice.id;   }   console.log(keepalldata); } 

demo


Comments