0

I have some JSON data and I need to replace one value with another string using Angular.js or JavaScript. My code is below:

$http({
    method:'POST',
    url:"php/getFilterCodeData.php",
    data:filterData,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).then(function successCallback(response){
    console.log('filter',response.data);
    $scope.last=[];
    for(var i=0;i<response.data.length;i++){
       $scope.arrCode=response.data[i].generated_code.split(',');
    }
        //console.log('arr',$scope.arrCode);
    for(var i=0;i<$scope.arrCode.length;i++){
        $scope.last.push($scope.arrCode[i].split("_").pop());
    }
    //console.log('last',$scope.last);
    var newStr=$scope.last[0]+"-"+$scope.last[$scope.last.length-1];

    //console.log('new str',newStr);
},function errorCallback(response) {
})

Here I am getting the below data using console:

filter [{
customer_name: "Big-Bazar"
expired_date: "23-12-2015"
generated_code: "medilink_global_01,medilink_global_02,medilink_global_03,medilink_global_04,medilink_global_05,medilink_global_06,medilink_global_07,medilink_global_08,medilink_global_09,medilink_global_10,medilink_global_11,medilink_global_12,medilink_global_13,medilink_global_14,medilink_global_15,medilink_global_16,medilink_global_17,medilink_global_18,medilink_global_19,medilink_global_20,medilink_global_21,medilink_global_22,medilink_global_23,medilink_global_24,medilink_global_25,medilink_global_26,medilink_global_27,medilink_global_28,medilink_global_29,medilink_global_30,medilink_global_31,medilink_global_32,medilink_global_33,medilink_global_34,medilink_global_35,medilink_global_36,medilink_global_37,medilink_global_38,medilink_global_39,medilink_global_40"
no_of_voucher: "40"
status: "generated"
voucher_amount: "3000"
voucher_code_id: "13"}]

Here I need to replace the generated_code: value with newStr. The expected output should be:

generated_code:01-40
halfer
  • 19,824
  • 17
  • 99
  • 186
satya
  • 3,508
  • 11
  • 50
  • 130

2 Answers2

2

In the Javascript:

$scope.filter = filter;
$scope.filter[0].generate_code = newStr;

To show the string, just use {{ filter[0].generate_code }} in your template.

AngularJS always watches changes to variables in the scope and will replace them in the template as they change, so it's pretty straight forward.

JerVoo
  • 47
  • 8
0

you do it like this:

response.data[i].generated_code=newStr;
aitnasser
  • 1,216
  • 1
  • 9
  • 23