在推入阵列之前删除重复的值

时间:2017-11-09 05:08:35

标签: javascript angularjs

enter image description here

在图像中可以看到数组[0]和数组[3],数组1和数组[4]是相同的。我正在检查为什么值重复,但我失败了。因此,如果存在任何重复值,请考虑删除。我需要将其删除 $ scope.arr数组本身。

代码:

  $scope.arr = [];
planReq.then(function (callplanList) {
    $scope.callplanList = callplanList.data.callplans;
    //console.log($scope.callplanList);
    for(var i = 0; i < $scope.planMapping.length; i++){
        //console.log($scope.planMapping[i].PlanScode);         
        for(var j = 0; j < $scope.callplanList.length; j++){                
            if(($scope.planMapping[i].PlanScode == $scope.callplanList[j].un_s_code) && ($scope.callplanList[j].offer_type == "REC")){
                //console.log($scope.devicesList);
                for(var a = 0; a < $scope.callplanList[j].upfront_cost.length; a++){
                    if($scope.callplanList[j].upfront_cost[a].upfront != ""){
                        //console.log($scope.callplanList[j].upfront_cost[a].handsetClass);
                        for(var k = 0; k < $scope.devicesList.length; k++){
                            if($scope.callplanList[j].upfront_cost[a].handsetClass == $scope.devicesList[k].device_class.toLowerCase()){
                                $scope.arr.push($scope.devicesList[k]);                                 
                            }
                        }
                    }
                }
            }
        }


    }
    console.log($scope.arr);
});

任何帮助都将不胜感激。

4 个答案:

答案 0 :(得分:1)

使用过滤器

这是一个简单的过滤器,用于从数组中删除重复项

Array.filter(function(elem, index, self){ return self.indexOf(elem) == index })

在你的情况下,它将是

$scope.arr =  $scope.arr.filter(function(elem, index, self){ 
                                  return self.indexOf(elem) == index 
                                });

答案 1 :(得分:0)

Product Edit form

dfs.datanode.data.dir =/mnt/datadisk1,/mnt/datadisk2,/mnt/datadisk3

dfs.datanode.data.dir = /mnt/datadisk1,/mnt/datadisk2

它返回基于所有属性的唯一数组

method 1

如果要从数组中删除重复元素,请使用以下函数。这里的args是: myArr:包含数组ob对象 prop:应该删除数组元素的对象的属性

var list =[{name:"a",age:2}, {name:"b",age:4}, {name:"c",age:6}, {name:"a",age:2}]
var arr = list.filter((elem, index, self) => self.findIndex(
    (t) => {return (t.name === elem.name && t.age === elem.age)}) === index)

它将根据elements属性删除重复项,并使用唯一的元素返回数组。

答案 2 :(得分:0)

ngRepeat使用$ watchCollection来检测集合中的更改。当发生更改时,ngRepeat会对DOM进行相应的更改:

添加项目时,会将新模板实例添加到DOM。 删除项目后,将从DOM中删除其模板实例。 重新排序项目时,它们各自的模板将在DOM中重新排序。 为了最大限度地减少DOM元素的创建,ngRepeat使用一个函数来“跟踪”集合中的所有项及其相应的DOM元素。例如,如果项目被添加到集合中,则ngRepeat将知道所有其他项目已经具有DOM元素,并且不会重新呈现它们。

默认跟踪功能(按标识跟踪项目)不允许数组中的重复项目。这是因为当存在重复项时,不可能在集合项和DOM元素之间保持一对一的映射。

如果您确实需要重复重复项目,则可以使用跟踪表达式替换默认跟踪行为。

所以你可以这样做:

$sql = "Select * FROM books Group By names"; 
    $result = $conn->query($sql);


    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            $sql1 = "SELECT * FROM books WHERE names = '" . $row["names"]. "' ORDER BY book ASC"; 
            $result1 = $conn->query($sql1);
            if ($result1->num_rows > 0) {
                $newbook = '1';
                // output data of each row
                while($row1 = $result1->fetch_assoc()) {

                    if ($row1["book"] == $newbook) {
                        echo "Exist<br><br>";
                        $newbook = $newbook+ 1;
                    } else {
                        if ($row1["book"] == '1' || $row1["book"] == '10' || $row1["book"] == '17') {
                            $newbook= $newbook+ 1;

                        } else {
                            echo "Add".$newbook."<br><br>";
                            break;
                        }
                    }
                }
            }
        }
    }

如果要从数组中删除重复项:

<div ng-repeat="n in [42, 42, 43, 43] track by $index">
 {{n}}
 </div>

这是小提琴:

function UniqueArraybyId(collection, keyname) {
              var output = [], 
                  keys = [];

              angular.forEach(collection, function(item) {
                  var key = item[keyname];
                  if(keys.indexOf(key) === -1) {
                      keys.push(key);
                      output.push(item);
                  }
              });
        return output;
   }; 
planReq.then(function (callplanList) {
    $scope.callplanList = callplanList.data.callplans;
    //console.log($scope.callplanList);
    for(var i = 0; i < $scope.planMapping.length; i++){
        //console.log($scope.planMapping[i].PlanScode);         
        for(var j = 0; j < $scope.callplanList.length; j++){                
            if(($scope.planMapping[i].PlanScode == $scope.callplanList[j].un_s_code) && ($scope.callplanList[j].offer_type == "REC")){
                //console.log($scope.devicesList);
                for(var a = 0; a < $scope.callplanList[j].upfront_cost.length; a++){
                    if($scope.callplanList[j].upfront_cost[a].upfront != ""){
                        //console.log($scope.callplanList[j].upfront_cost[a].handsetClass);
                        for(var k = 0; k < $scope.devicesList.length; k++){
                            if($scope.callplanList[j].upfront_cost[a].handsetClass == $scope.devicesList[k].device_class.toLowerCase()){
                                $scope.arr.push($scope.devicesList[k]);                                 
                            }
                        }
                    }
                }
            }
        }


    }

   $scope.arr = UniqueArraybyId($scope.arr ,"sub_family"); //you have to pass the key name 


    console.log($scope.arr);
})
function UniqueArraybyId(collection, keyname) {
              var output = [], 
                  keys = [];

              angular.forEach(collection, function(item) {
                  var key = item[keyname];
                  if(keys.indexOf(key) === -1) {
                      keys.push(key);
                      output.push(item);
                  }
              });
        return output;
   }; 
   
function test () {
    
    var arr =[{sub_family:'j3 (2016)'},{sub_family:'j3 (2016)'},{sub_family:'j3 (2017)'}]
     arr= UniqueArraybyId(arr ,"sub_family"); //you have to pass the key name 

    
    console.log(arr);
};

test();

答案 3 :(得分:0)

我认为这是从数组中删除重复项的最佳方法。请注意,这需要ES6。

arr = Array.from(new Set(arr));

示例:

&#13;
&#13;
var arr = [0, 1, 1, 1, 1, 2, 3, 4, 3, 4, 2, 2, 4];
console.log(arr.join(", "));
arr = Array.from(new Set(arr));
console.log(arr.join(", "));
&#13;
&#13;
&#13;