【问题标题】:Handling Response datas and moving into array处理响应数据并移入数组
【发布时间】:2016-09-08 21:24:57
【问题描述】:

我收到了来自后端的回复。我需要在下拉列表中传递基于所选国家的州和基于下拉列表中所选州的城市。 我可以将它们推入 array ,但它正在被替换。例如。Country India 也将有 USA states,它已被替换为 india states。需要帮助。

回应:

    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];
}
});
}

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    只需将您的数据转换为具有类似格式的对象数组

    { country, state, city }
    

    使用 lodash 的示例转换:

    var data = [];
    var cities = _.flatMap(data, mapCountries);
    
    function mapCountries(country){
       return _.flatMap(country.states, mapStates).map(setAttr('country', country.countryname));
    }
    
    function mapStates(state){
       return state.cities.map(setAttr('state', state.statename));
    }
    
    function setAttr(key, value){
    
      return function(obj){
         obj[key] = value;
         return obj;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 2016-09-21
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      相关资源
      最近更新 更多