不在数组中推送重复对象

时间:2017-10-10 05:41:20

标签: angularjs

我有json技能,我需要从下拉列表中model.skills多选技能,我使用multipleSkills()推送arrayvalues[]中的选定值,但我不应该将重复值推入arrayvalues

杰森:

var skills = 
[{"name":{"skillId":1,"skillName":"JAVA"}},
{"name":{"skillId":2,"skillName":"C"}},
{"name":{"skillId":3,"skillName":"DEVOPS"}},
{"name":{"skillId":4,"skillName":"ANGULAR JS"}},
{"name":{"skillId":41,"skillName":"drupal"}},
{"name":{"skillId":42,"skillName":"backbone js"}},
{"name":{"skillId":43,"skillName":"nodejs"}},
{"name":{"skillId":44,"skillName":"phone gap"}},
{"name":{"skillId":45,"skillName":"scala"}},
{"name":{"skillId":46,"skillName":"spark"}}]

HTML:

<input class="form-control1" type="text" name="skills" placeholder="Core Skills"
       ng-model="model.Skills"  ng-focus="skillsforJobs(model.Skills)" 
       typeahead ="skill as skill.name.skillName for skill in skills | filter:$viewValue | limitTo:10"
       typeahead-on-select="multipleSkills(model.Skills)"
       ng-minlength =1 />

JS:

    $scope.arrayvalues = [];
    $scope.multipleSkills = function(data){
        if($scope.arrayvalues.length <1){
              $scope.arrayvalues.push(data);
          } else if($scope.arrayvalues.length > 0){
             if($scope.arrayvalues.indexOf($scope.model.Skills) == -1) {                
              if($scope.model.Skills != ""){
                   for( var i=0; i<$scope.arrayvalues.length;i++){

              $scope.arrayvalues[i].name.skillId.indexOf( data.name.skillId){
              $scope.arrayvalues.push(data);
                                  $scope.model.Skills = "";

                    }

                    }

        } 

        }

1 个答案:

答案 0 :(得分:0)

请参考这个github项目。 AngularJS Dropdown Multiselect

HTML

<script type="text/javascript" src="angularjs-dropdown-multiselect.js"></script>            
    <div ng-app="myApp" ng-controller="myCtrl">
           <div ng-dropdown-multiselect="" options="example1data" selected-model="example1model"></div>    
            </div>  

JS

var app = angular.module('myApp', ['angularjs-dropdown-multiselect']);
app.controller('myCtrl', function($scope) {

});