Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Angularjs数组在拼接后更改引用_Arrays_Angularjs - Fatal编程技术网

Arrays Angularjs数组在拼接后更改引用

Arrays Angularjs数组在拼接后更改引用,arrays,angularjs,Arrays,Angularjs,我正在为我的项目使用angularjs,它运行良好。今天,我面临的问题很少,但不同 所以我需要一些建议。 考虑代码: $scope.deleteEmpty = function(){ for(var sp=0;sp < $scope.column.length;sp++){ var offerings = $scope.column[sp]; if(offerings.spName == -1){ $scope.rem

我正在为我的项目使用angularjs,它运行良好。今天,我面临的问题很少,但不同 所以我需要一些建议。 考虑代码:

$scope.deleteEmpty = function(){

    for(var sp=0;sp < $scope.column.length;sp++){

        var offerings = $scope.column[sp];

        if(offerings.spName == -1){

            $scope.removeanswer(sp);                        
        }
    }
},

$scope.removeanswer = function(position){

    for(var question=1;question<$scope.rows.length;question++){

        $scope.rows[question].answerlst.splice(position, 1);
    }
},
第一次调用$scope.removeanswersp;它删除了第一列的answerlst,但之后answerlst的位置会发生变化。所以它删除了第一和第三个位置,而不是整个位置

有什么建议吗。
谢谢。

如果有更多信息,可以改进以下解决方案

其思想是将列索引存储在一个toRemove数组中,然后从每个问题的答案中一次将它们全部删除

$scope.deleteEmpty = function(){
    var toRemove = []; // column indexes to be removed

    for(var sp=0;sp < $scope.column.length;sp++){
        var offerings = $scope.column[sp];
        if(offerings.spName == -1){
            toRemove.push(sp);                        
        }
    }
    $scope.removeanswer(toRemove);
},

$scope.removeanswer = function(positions){
    for(var question=1;question<$scope.rows.length;question++){
        $scope.rows[question].answerlst = $scope.rows[question].answerlst.filter(function(value, index) {
          return positions.indexOf(index) < 0;
        });
    }
},

$scope.rows中有什么?建议您在jsfiddle.net或plnkr.co中创建一个演示,复制您的问题。很难从你的描述中理解问题是什么。我们也看不到您在哪里调用@Robin Elvin question中的deleteEmpty,其中包含答案列表。
$scope.deleteEmpty = function(){
    var toRemove = []; // column indexes to be removed

    for(var sp=0;sp < $scope.column.length;sp++){
        var offerings = $scope.column[sp];
        if(offerings.spName == -1){
            toRemove.push(sp);                        
        }
    }
    $scope.removeanswer(toRemove);
},

$scope.removeanswer = function(positions){
    for(var question=1;question<$scope.rows.length;question++){
        $scope.rows[question].answerlst = $scope.rows[question].answerlst.filter(function(value, index) {
          return positions.indexOf(index) < 0;
        });
    }
},