Ajax
Ajax

Reputation: 148

Angular: How to filter json with specific keys

I am getting a json from multiselect items which are checked. How do I filter json based on specific keys only from the below original json:

Original Json:

 {
        "Reward": [{
            "RewardType": 1,
            "RewardTypeValue": "PartCodes",
            "RewardCode": "CB_USD_20_US",
            "DiscountValue": 20.0
        },
        {
            "RewardType": 1,
            "RewardTypeValue": "PartCodes",
            "RewardCode": "CB_USD_30_US",
            "DiscountValue": 30.0
        },
        {
            "RewardType": 1,
            "RewardTypeValue": "PartCodes",
            "RewardCode": "CB_USD_40_US",
            "DiscountValue": 40.0
        },
        {
            "RewardType": 1,
            "RewardTypeValue": "PartCodes",
            "RewardCode": "CB_USD_50_US",
            "DiscountValue": 50.0
        }]

    }

At this below code, I am trying to get only RewardType,RewardCode and DiscountType before calling "push" for "selectedRewards"

Angular Code:

  $scope.rewards = [];
$scope.toggleSelection = function toggleSelection(selectedRewards) {
   var idx = $scope.rewards.indexOf(selectedRewards);
   console.log(selectedRewards);

   // is currently selected
   if (idx > -1) {
     $scope.rewards.splice(idx, 1);
   }

   // is newly selected
   else {

     $scope.rewards.push(selectedRewards);
   }
   console.log($scope.rewards);
};

Html code:

<li ng-repeat="reward in RewardsList ">
           <input id="{{reward.RewardCode}}.{{reward.RewardType}}"
                  type="checkbox"
                  ng-checked="selection.indexOf(reward.RewardCode) > -1"
                  ng-click="toggleSelection(reward)"
                  value="{{reward.RewardCode}}" /> 
    <label  for="{{reward.RewardCode}}">{{reward.RewardCode}}</label>
    </li>

here is the expected json I am trying to get:

{
        "Reward": [{
            "RewardType": 1,
            "RewardCode": "CB_USD_20_US",
            "DiscountValue": 20.0
        },
        {
            "RewardType": 1,
            "RewardCode": "CB_USD_30_US",
            "DiscountValue": 30.0
        },
        {
            "RewardType": 1,
            "RewardCode": "CB_USD_40_US",
            "DiscountValue": 40.0
        },
        {
            "RewardType": 1,
            "RewardCode": "CB_USD_50_US",
            "DiscountValue": 50.0
        }]

    }

Upvotes: 0

Views: 1345

Answers (2)

Ajax
Ajax

Reputation: 148

The following fixed my issue:

Code:

$scope.rewards.push({"RewardType":selectedRewards.RewardType, "RewardCode":selectedRewards.RewardCode," "DiscountValue":selectedRewards.DiscountValue});

Upvotes: 1

ValLeNain
ValLeNain

Reputation: 2304

I don't really get what is the variable which stores the json in your code, but let's say your variable is called myJson and corresponds to the original json you've shared. To get the expected json you'd do:

var expectedJson = {
  Reward: myJson.Reward.map(function(item) {
    return {
     RewardType: item.RewardType,
     RewardCode: item.RewardCode,
     DiscountValue: item.DiscountValue
    }
  })
}

Have a look at Array.prototype.map if you're not familiar with it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Upvotes: 2

Related Questions