ASHOK ASHOK - 3 months ago 12
AngularJS Question

How to add an attribute to each object in an array?

My array is like this

"taxDetails": [
{
"taxType": "Flat Service Charge",
"taxAmount": 0
},
{
"taxTypeId": "1",
"taxType": "Service Tax",
"validFrm": "2016-01-18",
"validTo": "2020-02-27",
"taxPrctgSbt": 200,
"taxPrctg": 14.5,
"taxAmount": 300,
"remarks": "test"
},
{
"taxTypeId": "2",
"taxType": "VAT",
"validFrm": "2016-01-18",
"validTo": "2020-02-29",
"taxPrctgSbt": 300,
"taxPrctg": 12.5,
"taxAmount": 400,
"remarks": "rest"
},
{
"taxTypeId": "3",
"taxType": "Swatch Bharath Cess",
"validFrm": "2016-01-18",
"validTo": "2020-03-31",
"taxPrctgSbt": 400,
"taxPrctg": 1,
"taxAmount": 500,
"remarks": "fest"
},
{
"taxTypeId": "5",
"taxType": "Swatch Bharath Cess",
"validFrm": "2016-01-18",
"validTo": "2020-03-31",
"taxPrctgSbt": 400,
"taxPrctg": 1,
"taxAmount": 500,
"remarks": "fest"
},
{
"taxTypeId": "6",
"taxType": "Percent Service Char",
"validFrm": "2016-01-18",
"validTo": "2020-08-01",
"taxPrctgSbt": 200,
"taxPrctg": 10,
"taxAmount": 200,
"remarks": "zest"
},
{
"taxTypeId": "7",
"taxType": "Percent Service Char",
"validFrm": "2016-01-18",
"validTo": "2020-08-01",
"taxPrctgSbt": 300,
"taxPrctg": 15,
"taxAmount": 200,
"remarks": "zest"
}
]


and my code is

$scope.paymentForm.taxDetails = [];
//$scope.taxDetails = [];
var flat= {"flat" : true};
$scope.taxList = function () {
$http.get('http://192.168.0.113:8080/feasthunt/registration/getTaxDetails?restUniqCode='+uniqueCode)
.success(function (response) {

$scope.paymentForm.taxDetails = response;

for(var i=0; i< $scope.paymentForm.taxDetails.length; i++){
$scope.paymentForm.taxDetails[i].push( flat );
}
})
.error(function (data, status, header, config) {
//alert('error');
});
};
$scope.taxList();


I want add another object in every array of elements. And final result will be like this

"taxDetails": [
{
"taxType": "Flat Service Charge",
"taxAmount": 0,
"flat": true
},
{
"taxTypeId": "1",
"taxType": "Service Tax",
"validFrm": "2016-01-18",
"validTo": "2020-02-27",
"taxPrctgSbt": 200,
"taxPrctg": 14.5,
"taxAmount": 300,
"remarks": "test",
"flat": true
},
{
"taxTypeId": "2",
"taxType": "VAT",
"validFrm": "2016-01-18",
"validTo": "2020-02-29",
"taxPrctgSbt": 300,
"taxPrctg": 12.5,
"taxAmount": 400,
"remarks": "rest",
"flat": true
},
{
"taxTypeId": "3",
"taxType": "Swatch Bharath Cess",
"validFrm": "2016-01-18",
"validTo": "2020-03-31",
"taxPrctgSbt": 400,
"taxPrctg": 1,
"taxAmount": 500,
"remarks": "fest",
"flat": true
},
{
"taxTypeId": "5",
"taxType": "Swatch Bharath Cess",
"validFrm": "2016-01-18",
"validTo": "2020-03-31",
"taxPrctgSbt": 400,
"taxPrctg": 1,
"taxAmount": 500,
"remarks": "fest",
"flat": true
},
{
"taxTypeId": "6",
"taxType": "Percent Service Char",
"validFrm": "2016-01-18",
"validTo": "2020-08-01",
"taxPrctgSbt": 200,
"taxPrctg": 10,
"taxAmount": 200,
"remarks": "zest",
"flat": true
},
{
"taxTypeId": "7",
"taxType": "Percent Service Char",
"validFrm": "2016-01-18",
"validTo": "2020-08-01",
"taxPrctgSbt": 300,
"taxPrctg": 15,
"taxAmount": 200,
"remarks": "zest",
"flat": true
}
]

Answer

as i understand, you want to set attribute flat=true for each array element. Maybe following code could work:

for(var i=0; i< $scope.paymentForm.taxDetails.length; i++){
  $scope.paymentForm.taxDetails[i].flat = true;
}
Comments