访问数组内对象的值

时间:2017-10-15 10:39:51

标签: javascript angularjs arrays json

我对http请求的回复很复杂,如下所示:

"rates": [
            {
                "non_refundable": false,
                "no_of_rooms": 1,
                "includes_wifi": false,
                "includes_boarding": true,
                "has_promotions": true,
                "group_code": "xgirbylkwb6xtg",
                "currency": "INR",
                "cancellation_policy": {
                    "under_cancellation": false,
                    "details": [
                        {
                            "from": "2016-12-25T00:00:00",
                            "flat_fee": 20309.01,
                            "currency": "INR"
                        }
                    ],
                    "cancel_by_date": "2016-12-24T00:00:00",
                    "amount_type": "value"
                }
           }
      ]

长度在6到20之间,在另一个阵列内,长度约为200。 "cancellation_policy"仅在"non_refundable": false

时显示

我如何访问"from": "2016-12-25T00:00:00"并使用Moment JS更改格式以及如何访问"flat_fee": 20309.01

我已经尝试使用angular.forEach,但它只定义了最后一个数组的值。

我已经尝试过了:

 if ($scope.data[i].rates[j].non_refundable == false) {
for (var d = $scope.data[i].rates[j].cancellation_policy.details.length - 1; d >= 0; d--) {

但我没有成功。

PS: 该值应显示在$mdDialog

1 个答案:

答案 0 :(得分:1)

你不需要循环细节,只需这样做:

var startdate = $scope.data[i].rates[j].cancellation_policy.details[0].from;
var new_date = moment(startdate).format('MMMM Do YYYY, h:mm:ss a');
var other = $scope.data[i].rates[j].cancellation_policy.details[0].flat_fee;