如何为数组中的每个对象添加属性?

时间:2016-08-09 11:00:36

标签: javascript angularjs

我的数组就像这样

"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"
}
]

我的代码是

$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();

我想在每个元素数组中添加另一个对象。最终结果将是这样的

"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
}
]

2 个答案:

答案 0 :(得分:1)

据我所知,你想为每个数组元素设置属性flat = true。 也许以下代码可以工作:

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

答案 1 :(得分:0)

您正在尝试将新属性推送到对象,就像它是一个数组一样。您应该设置新属性。

变化:

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

要:

for(var i=0; i< $scope.paymentForm.taxDetails.length; i++){
    $scope.paymentForm.taxDetails[i]["flat"] = true; //or what ever value you want
}

您也可以使用for-in循环来缩短代码:

for(var i in $scope.paymentForm.taxDetails)
    $scope.paymentForm.taxDetails[i]["flat"] = true;