简体   繁体   中英

Check if an element exists in a object

I have a set of radio buttons, when a radio button is selected the element is pushed into an object.

Then I need to take the value of the elements in the object and then push them into an array, if they are not already in the array.

My problem is that I keep getting duplicates or more in my array of each of the values with this function because I am not checking correctly if they exists.

How can I check if the element already exists in my array and then exclude it from being added?

      _this.selected = {};
      _this.selectedArray = [];

      //loop through the object
      angular.forEach(_this.selected, function ( item ){

        //if it is not the first time the array is getting data loop through the array
        if(_this.selectedArray.length > 0) {
          _this.selectedArray.forEach(function (node){

            //check to see if the item already exists-- this is where it is failing
            //and running the loop too many times 
            if(node.id !== item.id){
              _this.selectedArray.push(item);
            }
          });
        } else {
          _this.selectedArray.push(share);
        }
      });

You can use additional hash to check if you have already added item to array.

  _this.selected = {};
  _this.selectedArray = [];
  _this.selectedHash = {};

  //loop through the object
  angular.forEach(_this.selected, function ( item ){
      if(_this.selectedHash[item.id]) { return; }

      _this.selectedArray.push(item);
      _this.selectedHash[item.id] = 1;         
  });

Have you tried jQuery's inArray ? http://api.jquery.com/jquery.inarray

It works the same as the indexOf method on String .

You could try if $.inArray(node, selectedArray) == -1 // element is not in the array

You can run this function on that array to reduce it to unique values rather than applying complex code to check whether the array had an element or not

var getOnlyUniqueValuesInArray = function(arrayObj) {
    return arrayObj.reduce(function(a, b) {
        if (a.indexOf(b) < 0) a.push(b);
        return p;
    }, []);
};

Hi that should helps

var app = angular.module('app', []);

app.controller('firstCtrl', function($scope){


var app = angular.module('app', []);

app.controller('firstCtrl', function($scope) {
  _this.selected = {};
  _this.selectedArray = [];

  //loop through the object
  angular.forEach(_this.selected, function(item) {

    //if it is not the first time the array is getting data loop through the array
    if (_this.selectedArray.length > 0) {

      var canAdd = true;
      _this.selectedArray.forEach(function(node) {

        //do not add iny any of node.id will be equal to item.id
        if (node.id == item.id) {
          canAdd = false;
        }

      });
      if(canAdd){
        _this.selectedArray.push(item);
      };
    } else {
      _this.selectedArray.push(share);
    }
  });

})

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM