Angularjs Angular:如何使用特定键过滤json
我从多个选中的项目中得到一个json。如何仅根据以下原始json中的特定键过滤json: 原始Json:Angularjs Angular:如何使用特定键过滤json,angularjs,aem,aem-6,Angularjs,Aem,Aem 6,我从多个选中的项目中得到一个json。如何仅根据以下原始json中的特定键过滤json: 原始Json: { "Reward": [{ "RewardType": 1, "RewardTypeValue": "PartCodes", "RewardCode": "CB_USD_20_US", "DiscountValue": 20.0 }, {
{
"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
}]
}
在下面的代码中,我试图在调用“selectedRewards”的“push”之前只获取RewardType、RewardCode和DiscountType
角度代码:
$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代码:
<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>
我不知道在代码中存储json的变量是什么,但假设您的变量名为
myJson
,与您共享的原始json相对应。要获得预期的json,您需要执行以下操作:
var expectedJson = {
Reward: myJson.Reward.map(function(item) {
return {
RewardType: item.RewardType,
RewardCode: item.RewardCode,
DiscountValue: item.DiscountValue
}
})
}
如果您不熟悉
Array.prototype.map
,请查看它:我不知道在代码中存储json的变量是什么,但假设您的变量名为myJson
,与您共享的原始json相对应。要获得预期的json,您需要执行以下操作:
var expectedJson = {
Reward: myJson.Reward.map(function(item) {
return {
RewardType: item.RewardType,
RewardCode: item.RewardCode,
DiscountValue: item.DiscountValue
}
})
}
如果您不熟悉,请查看
Array.prototype.map
:以下解决了我的问题:
代码:
$scope.rewards.push({"RewardType":selectedRewards.RewardType, "RewardCode":selectedRewards.RewardCode," "DiscountValue":selectedRewards.DiscountValue});
以下解决了我的问题: 代码:
$scope.rewards.push({"RewardType":selectedRewards.RewardType, "RewardCode":selectedRewards.RewardCode," "DiscountValue":selectedRewards.DiscountValue});
没有足够的了解或显示。我们不知道
selectedewards
来自何处,也不知道预期的结果是什么。请提供更详细的解释,预期结果是json减去“RewardTypeValue”键/值。抱歉…请更具体地添加预期结果预期json。只需在其上循环,并在该属性上使用delete
。如果只想在副本上删除,请使用angular.copy()。我们不知道selectedewards
来自何处,也不知道预期的结果是什么。请提供更详细的解释,预期结果是json减去“RewardTypeValue”键/值。抱歉…请更具体地添加预期结果预期json。只需在其上循环,并在该属性上使用delete
。如果只想在副本上删除,请使用angular.copy()
获取消息clientlibs.min.js:formatted:3289 TypeError:无法读取未定义的属性“map”。我使用angular 1.4回答您的第一个问题:selectedRewards将接收原始json。这是一个动态调用,因此未定义数组json大小。因此,在填充selectedRewards
变量(我会说在推送之前是juste)获取消息clientlibs.min.js:formatted:3289 TypeError:Cannot read property'map'of undefined之后,调用我编写的几行代码。我使用angular 1.4回答您的第一个问题:selectedRewards将接收原始json。这是一个动态调用,因此没有定义数组json大小。因此,在填充selectedRewards
变量之后,您调用了我编写的几行代码(我说的是just before to push)