处理响应数据并移入阵列

时间:2016-09-08 12:38:14

标签: angularjs

我有来自后端的回复。我需要在下拉列表中根据所选国家/地区传递状态,并根据我的表单下拉列表中的选定状态传递城市。 我能够将它们推入阵列,但它正在被替换。例如.Country India也将拥有美国的州,它已被印度州取代。需要援助。

回应:

    Geography :
[{"countryname":"India",
"states":[{"statename":"Karnataka",
"cities":[{"city":"Bangalore","segments":[{"segment":""}]},
{"city":"Hubli","segments":[{"segment":""}]}]},
{"statename":"Tamil Nadu",
"cities":[{"city":"Chennai","segments":[{"segment":""}]},
{"city":"Coimbatore","segments":[{"segment":""}]}]}]},

{"countryname":"USA",
"states":[{"statename":"California",
"cities":[{"city":"San Francisco","segments":[{"segment":""}]},{"city":"San Jose","segments":[{"segment":""}]}]},
{"statename":"New Jersey",
"cities":[{"city":"Princeton","segments":[{"segment":""}]},
{"city":"South Brunswick","segments":[{"segment":""}]}]}]}]

JS:

    UserService.Geography(json).then(function(response) {

$scope.model.countries = [];
$scope.model.countries.push("ALL");

if (response.json.response.statuscode == 0 && response.json.response.statusmessage == 'Success') {
var geography = response.json.response.geography;
console.log("Geography : " + JSON.stringify(geography));
$scope.geography = geography;

for (var i = 0; i < geography.length; i++) {

$scope.model.countries.push(geography[i].countryname);
console.log($scope.model.countries);

if (($scope.model.countries != []) || ($scope.model.countries != null)) {
$scope.model.states = [];
$scope.model.states.push("ALL");
for (var j = 0; j < geography[i].states.length; j++) {
if (geography[i].states) {
$scope.model.states.push(geography[i].states[j].statename);
} else {
$scope.model.states.push(geography[i].state[j].statename);
}
console.log($scope.model.states);
if (($scope.model.states != []) || ($scope.model.states != null)) {
$scope.model.cities = [];
$scope.model.cities.push("ALL");
// for first time combobox loading,
// load only cities for first state
if (j === 0) {
for (var k = 0; k < geography[i].states[j].cities.length; k++) {
    console.log('======k=====:'+k);
    console.log('geography[i].states[j].cities[k].city=====:'+geography[i].states[j].cities[k].city);
    if (geography[i].states) {
       $scope.model.cities.push(geography[i].states[j].cities[k].city);
    } else {
       $scope.model.cities.push(geography[i].state[j].cities[k].city);
    };
    console.log('$scope.model.cities: '+ $scope.model.cities);

    if (($scope.model.cities != []) || ($scope.model.cities != null)) {

        $scope.model.segments = "_ALL";
        console.log($scope.model.segments);

    }
}
}
$scope.model.selectedCity = $scope.model.cities[0];
}
}
$scope.model.selectedState = $scope.model.states[0];
}
}
$scope.model.selectedCountry = $scope.model.countries[0];
}
});
}

1 个答案:

答案 0 :(得分:0)

只需将数据转换为格式为

的对象数组
s = S3Bucket(bucket, access_key=access_key, secret_key=secret_key)
print s  
<S3Bucket ... at 'https://s3.amazonaws.com/...'>

使用lodash转换示例:

{ country, state, city }