【问题标题】:Check if an element exists in a object检查对象中是否存在元素
【发布时间】:2014-07-30 14:23:32
【问题描述】:

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

然后我需要获取对象中元素的值,然后将它们推送到数组中,如果它们尚未在数组中。

我的问题是,我使用此函数在每个值的数组中不断重复或更多,因为我没有正确检查它们是否存在。

如何检查该元素是否已存在于我的数组中,然后将其排除在添加之外?

      _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);
        }
      });

【问题讨论】:

  • "被推入一个对象" 怎么样? “推入对象”是什么意思? :)
  • 我的意思是对象是用键值对更新的。
  • 您是否使用 id 作为键?如果是,那怎么可能有重复?
  • node 一个元素或一个对象。
  • 'node' 是一个对象,它们都是对象,我正在比较每个对象的 id。

标签: javascript arrays angularjs object


【解决方案1】:

您可以使用额外的哈希来检查您是否已经将项目添加到数组中。

  _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;         
  });

【讨论】:

    【解决方案2】:

    你试过jQuery的inArray吗? http://api.jquery.com/jquery.inarray

    它的工作原理与String 上的indexOf 方法相同。

    你可以试试if $.inArray(node, selectedArray) == -1 // element is not in the array

    【讨论】:

    • 他可能有 jQuery,但可能不太可能,因为他使用的是 angularjs。
    • 我原以为使用 Angular 的人很可能也在使用 jQuery。
    • 好吧,考虑到围绕 angular 的重点是让您的 javascript 不直接与 dom 交互,jquery 对 angularjs 变得非常无用(除了一些辅助方法,例如这个)
    • 根据他的 cmets 的说法,这个解决方案无论如何都不会起作用,因为他正在使用对象。回到绘图板,:)
    【解决方案3】:

    您可以在该数组上运行此函数以将其减少为唯一值,而不是应用复杂的代码来检查数组是否有元素

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

    【讨论】:

      【解决方案4】:

      你好,应该有帮助

      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);
          }
        });
      
      })
      
      });
      

      【讨论】:

      • 你没有使用canAdd
      • 没问题。顺便说一句,由于其他原因,这仍然不起作用。首先:您在迭代 selectedArray 时正在更改它。
      • @YuryTarabanko if(canAdd).. 应该在循环块之后,而不是在里面。我已经更新了答案
      • 现在可以工作了。最后一个建议:由于您使用的是本机 forEach,因此当您需要根据某些谓词检查数组元素时,看起来 someevery 更适合。 !selectedArray.some(function(node) {return node.id == item.id;})selectedArray.every(function(node) {return node.id != item.id;}) :)
      猜你喜欢
      • 2019-10-12
      • 2017-09-17
      • 1970-01-01
      • 2012-11-20
      • 1970-01-01
      相关资源
      最近更新 更多