Javascript 复制要添加到同一数组的最后一个数组对象

Javascript 复制要添加到同一数组的最后一个数组对象,javascript,arrays,angularjs,Javascript,Arrays,Angularjs,我希望这很简单。我正在尝试将数组中的最后一个对象再次添加到数组中 像这样: arr = [1, 2] result = [1, 2, 2] 只需使用以下代码: $scope.template = { ressource: { level: [{ gain: [], cost: [] }] } }; $scope.addLev

我希望这很简单。我正在尝试将数组中的最后一个对象再次添加到数组中

像这样:

arr = [1, 2]

result = [1, 2, 2]
只需使用以下代码:

    $scope.template = {
        ressource: {
            level: [{
                gain: [],
                cost: []
            }]
        }
    };

    $scope.addLevel = function() {
        var last = $scope.template.ressource.level[$scope.template.ressource.level.length - 1];
        $scope.template.ressource.level.push(last);
    };
但是我得到了这个错误:

错误:[ngRepeat:dupes]中继器中不允许重复。使用“跟踪依据”表达式指定唯一关键点。Repeater:template.ressource.level中的级别,重复键:对象:51,重复值:{“增益”:[],“成本”:[]}


编辑

因此,我尝试了
跟踪
,但我似乎没有得到任何新的东西,因此这里是我代码的
HTML
部分,我使用
增益
成本
值:

            <div ng-show="template.type === 'building'" class="vertical-spacing">
            <label>Ressource gain per level</label>

            <ul class="list-group">
                <li ng-repeat="level in template.ressource.level track by $id(level)" 
                    class="list-group-item" 
                    ng-hide="template.ressource.level.indexOf(level) === 0">
                    <span>
                        Level: {{ template.ressource.level.indexOf(level) }}
                    </span>

                    <span class="pull-right">
                        <a href="" ng-click="removeLevel(level)">
                            <span class="glyphicon glyphicon-remove"></span>
                        </a>
                    </span>

                    <ul>
                        <li class="list-group-item" ng-repeat="gain in level.gain track by $id(gain)">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>Amount</label>
                                    <input type="number" 
                                           class="form-control"
                                           ng-model="gain.amount" 
                                           min="1" 
                                           required>
                                </div>

                                <div class="form-group">
                                    <label>Ressource</label>
                                    <select required 
                                            ng-model="gain.ressource" 
                                            ng-init="gain.ressource = gain.ressource || ressources.basic[0]"
                                            ng-options="ressource._id as ressource.name | capitalize for ressource in ressources.basic"
                                            class="form-control selectWidth">
                                        <option style="display:none" value="" disabled>select a ressource</option>
                                    </select>
                                </div>

                                <span class="pull-right">
                                    <a href="" ng-click="removeGain(level, gain)">
                                        <span class="glyphicon glyphicon-remove"></span>
                                    </a>
                                </span>
                            </div>
                        </li>
                    </ul>

                    <span>
                        <a href="" ng-click="addGain(level)" ng-hide="(level.gain.length + 1) > ressources.basic.length">
                            <span class="glyphicon glyphicon-plus"></span> Add ressource
                        </a>
                    </span>
                </li>
            </ul>

            <span>
                <a href="" ng-click="addLevel()">
                    <span class="glyphicon glyphicon-plus"></span> Add level
                </a>
            </span>
        </div>

        <div ng-show="template.type === 'building'" class="vertical-spacing">
            <label>Ressource cost per level</label>

            <ul class="list-group">
                <li ng-repeat="level in template.ressource.level track by $id(level)" 
                    class="list-group-item" 
                    ng-hide="template.ressource.level.indexOf(level) === 0">
                    <span>
                        Level: {{ template.ressource.level.indexOf(level) }}
                    </span>

                    <span class="pull-right">
                        <a href="" ng-click="removeLevel(level)">
                            <span class="glyphicon glyphicon-remove"></span>
                        </a>
                    </span>

                    <ul>
                        <li class="list-group-item" ng-repeat="cost in level.cost track by $id(cost)">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>Amount</label>
                                    <input type="number" 
                                           class="form-control"
                                           ng-model="cost.amount" 
                                           min="1" 
                                           required>
                                </div>

                                <div class="form-group">
                                    <label>Ressource</label>
                                    <select required 
                                            ng-model="cost.ressource" 
                                            ng-init="cost.ressource = cost.ressource || ressources.categories[0]"
                                            ng-options="ressource._id as ressource.name | capitalize for ressource in ressources.categories"
                                            class="form-control selectWidth">
                                        <option style="display:none" value="" disabled>select a ressource</option>
                                    </select>
                                </div>

                                <span class="pull-right">
                                    <a href="" ng-click="removeCost(level, cost)">
                                        <span class="glyphicon glyphicon-remove"></span>
                                    </a>
                                </span>
                            </div>
                        </li>
                    </ul>

                    <span>
                        <a href="" ng-click="addCost(level)" ng-hide="(level.cost.length + 1) > ressources.categories.length">
                            <span class="glyphicon glyphicon-plus"></span> Add ressource
                        </a>
                    </span>
                </li>
            </ul>

            <span>
                <a href="" ng-click="addLevel()">
                    <span class="glyphicon glyphicon-plus"></span> Add level
                </a>
            </span>
        </div>

每级资源增益
  • 级别:{{template.ressource.Level.indexOf(Level)}
    • 数量 资源 选择一个资源
每个级别的资源成本
  • 级别:{{template.ressource.Level.indexOf(Level)}
错误现在表示:

错误:[ngRepeat:dupes]中继器中不允许重复。使用“跟踪依据”表达式指定唯一关键点。Repeater:template.ressource.level中的级别按$id跟踪(级别),重复键:对象:51,重复值:{“增益”:[],“成本”:[]}


你有一个角度误差。在ng repeat中使用
track by

<div ng-repeat="n in template.ressource.level track by $index">


参考文献

默认情况下,ngRepeat不允许数组中存在重复项。这是因为当存在重复项时,不可能在集合项和DOM元素之间保持一对一的映射

如果确实需要重复项目,可以使用“跟踪方式”表达式将默认跟踪行为替换为自己的跟踪行为


您的纯JavaScript代码和逻辑是正确的,这似乎是AngularJS的问题…我似乎无法让它工作。刚刚添加了我正在使用的
HTML
,也许这会对这件事有所帮助^^@MichaelTotKorsgaard Try
track by$index
hmm如果我尝试使用
$index
我没有得到错误,函数应该被调用。但是页面不会改变以允许添加新的级别。