我可以让一个对象与另一个对象具有相同的顺序吗?

时间:2016-08-05 21:07:33

标签: javascript angularjs arrays object

我知道这个问题的标题已经没有意义,因为对象的性质是无序的。但是,我想如果你看一下这里链接的屏幕截图,它会更有意义。

Picture of the two objects in my console.log

这是正在发生的事情。我正在创建一个名为$ scope.gameConfigs的对象,它本身是从我从服务器调用(配置对象)接收的数据创建的。这个$ scope.gameConfigs创建了一组下拉菜单。

基本上我要做的是让下拉菜单显示以前保存的数据,如果数据存在的话。保存的数据存在时,服务器返回包含所有已保存数据的对象。该数据按数字排序。我的问题是,这个保存的数据并不总是与我创建的$ scope.gameConfigs中的数据相同,这导致我的下拉列表中出现空白字段。

如果查看我链接的屏幕截图,您可以看到$ scope.gameConfigs中的键(其旁边写有第30行的对象)是Map,Server和Mode。

第二个对象,即从服务器返回的已保存数据,是一组对象,每个对象都有一个name属性(这是下拉菜单的名称)。如果您看一下,订单是服务器,地图和模式。

我的问题是,如何让Saved Data对象复制我的$ scope.gameConfigs对象的顺序?这甚至可能吗?如果没有,最好的方法是什么?

这也是我的页面/控制器的HTML和控制器,如果这有帮助的话:

<form class="form-horizontal" ng-controller="GamePreferenceCtrl">
    <div ng-repeat="(name, section) in gameConfigs">
        <label ng-bind="name"></label>
        <select class="form-control" ng-model="formData.settings[$index].value" ng-change="dropdownItemClicked(name, formData.settings[$index].value)">
            <option value="" disabled="disabled">---Please Select Option---</option>
            <option ng-repeat="item in section" value="{{item.value}}" ng-bind="item.value"></option>
        </select>
    </div>
    <div class="col-md-12" ng-include="gametemp"></div>
    <div class="row">
        <div class="hr-line-dashed"></div>
        <div class="text-center col-md-12 padding-15">
            <button class="btn btn-primary btn-lg" ng-click="saveGameSetting()" formnovalidate translate>
                <i class='fa fa-circle-o-notch fa-spin' ng-if="showBusy"></i>&nbsp;Save
            </button>
        </div>
    </div>
</form>

和控制器:

  function GamePreferenceCtrl($scope, $filter, Tournament, Notification) {
    $scope.$parent.child.game = $scope;
    $scope.selectedItems = [];
    $scope.formData = {};

    $scope.loadConfig = function () {
      Tournament.loadGameConfig($scope.id).then(function (response) {
        $scope.gameConfigs = {};

        if (response.data.tournamentPrefs) {
          _.each(response.data.tournamentPrefs, function (val) {
            if ($scope.formData.settings === undefined) {
              $scope.formData.settings = [];
            }
            $scope.formData.settings.push({section: val.name, value: val.value});
          });
          $scope.formData = {};
        };

        $scope.dropdownItemClicked = function(name, value) {
          if($scope.formData.settings === undefined) {
              $scope.formData.settings = {};
              $scope.formData.settings[name] = value;
          } else {
            console.log('inside else');
              $scope.formData.settings[name] = value;
          }

        };

        _.each(response.data.provisions, function (val) {
            if ($scope.gameConfigs[val.section] === undefined) {
              $scope.gameConfigs[val.section] = [];
            }
            $scope.gameConfigs[val.section].push({name: val.key, value: val.value});
        });
      });
    };

我知道这是一个很长的阅读,我真的很感激能够帮助我解决这个问题的人。谢谢!

1 个答案:

答案 0 :(得分:0)

你可能会喜欢

&#13;
&#13;
{{1}}
&#13;
&#13;
&#13;

相关问题