【问题标题】:Jquery/Javascript removing entry from array with highest property valueJquery/Javascript 从具有最高属性值的数组中删除条目
【发布时间】:2013-12-19 11:02:59
【问题描述】:

我有一个很好的谜语,我希望看到它被解开。可能有更好的方法可以做到这一点,我对想法持开放态度。 我正在尝试为画布绘图应用程序编写撤消功能。 我有以下对象,其中包含一个数组,其中包含具有三个属性的自己的对象。

var allDamages= {};
allDamages['scratch'] = [];
allDamages['scratch'].push({"x":4,"y":6,"index":1});
allDamages['scratch'].push({"x":3,"y":3,"index":2});
allDamages['scratch'].push({"x":9,"y":9,"index":3});
allDamages['scratch'].push({"x":19,"y":39,"index":4});
allDamages['dent'] = [];
allDamages['dent'].push({"x":59,"y":69,"index":5});
allDamages['dent'].push({"x":59,"y":69,"index":9});
allDamages['dent'].push({"x":39,"y":19,"index":6});
allDamages['rip'] = [];
allDamages['rip'].push({"x":20,"y":22,"index":7});
allDamages['rip'].push({"x":100,"y":56,"index":8});

我想从这个数组中删除最后一个条目。我想通过属性“索引”来做到这一点。 所以我需要以某种方式找到具有属性'index'最高值的条目,然后将其从数组中删除。这样做的最佳方法是什么?

您好,

罗伯特

【问题讨论】:

  • 如果数组是按索引排序的,你可以使用allDamages['scratch'].pop();,如果不是,你应该遍历数组获取索引最高的元素,然后使用allDamages['scratch'].splice(indexOfElement, 1);查看Array splice method和Array pop跨度>
  • 我得到的答案很接近,但不要给我正确的结果。我只想删除一个条目,而不是所有具有其域的最高索引属性的条目。所以我需要将 ["scratch"] 的最高索引与 ["dent"] 的最高索引进行比较,然后决定需要删除哪个条目。

标签: javascript jquery arrays properties highest


【解决方案1】:

allDamages.scratch.length -1 返回该数组的最后一个索引。

编辑:

allDamages.scratch.slice(-1).pop() 返回最后一个数组项。

如果你只想删除数组中的最后一项,你应该(就像 Givi 说的那样)在排序数组上使用 pop() 方法,如下所示:

allDamages['scratch'].pop()

编辑2:

因为我不清楚这个问题。这是我解决这个问题的最后一击。

var allDamagesInOneArray = [];

for(array in allDamages){
    allDamagesInOneArray.concat(array);//Assuming every key is an array
}

allDamagesInOneArray.sort(function(a,b){
    return a.index - b.index;
});

var lastObj = allDamagesInOneArray.slice(-1).pop(); //element with latest index

【讨论】:

  • 在这种情况下,您假设“索引”正在递增,就像在示例中一样
  • @douwe 如果他正在制作撤消功能,那么我猜他会删除最后一个索引。我将编写另一个完全涵盖它的解决方案。感谢您的反馈;)
  • 是的,我认为索引是指对象文字的索引属性,而不是数组索引。
  • 是的,正是属性“索引”的属性值
  • 除了你只看'scratch'。需要对所有(划痕、凹痕、裂纹、撕裂等)进行比较
【解决方案2】:

我认为您应该创建一个对象来保存您的三个属性。之后,您创建一个用于撤消的堆栈。像这样:

function yourObject(x,y,index){
   this.x = x; this.y = y; this.index = index;    
}

var yourStack = new Array();

yourStack.push(new yourObject(4, 6, 1));

【讨论】:

    【解决方案3】:

    如果数组中的最高索引始终是数组的最后一个元素:

    allDamages.scratch = allDamages.scratch.slice(0, allDamages.scratch.length - 1);
    

    这会删除数组的最后一个元素

    如果索引没有增加或者如果你总是想删除最新的索引,无论它在哪个损害数组中(我猜)你都可以使用这个函数:

    var undo = function(input){
    
        var max= 0;
        var undoType = "";
        var undoIndex = 0;
        for( var type in input ) {
            // type: string
    
            var locations = input[type];
            // locations: array
    
            // find the location of the heighest index property.
            for( var i = 0; i < locations.length; i++ ) {
                if( locations[i]["index"] > max) {
                    max = locations[i]["index"] ;
                    undoType = type;
                    undoIndex = index;
                }
            }
        }
    
        var output = input[type].splice(undoIndex, 1);
        return output;
    }
    

    这应该从损坏数组中删除具有最大“索引”属性的元素。

    【讨论】:

      【解决方案4】:

      首先,存储对象中最高 index 属性的计数器,以及该对象在 scratch 数组中的索引。

      var highestIndex = -Infinity;
      var indexInArray
      

      如果你使用 jQuery:

      $.each( allDamages.scratch, function highestIndex( index, object ){
        if( object.index > highestIndex ){
          highestIndex = object.index;
          indexInArray = index;
        }
      } );
      

      或者,如果不是:

      for( var indexCounter = 0, indexCounter < allDamages.scratch, indexCounter++ ){
        if( allDamanges.scratch[ indexCounter ].index > highestIndex ){
          highestIndex = allDamages.scratch[ indexCounter ].index;
          indexInArray = indexCounter;
        }
      };
      

      【讨论】:

      • 感谢您的回复。这很接近。但是你不知道数组'scratch'是否具有整个对象'allDamages'的最高'index'属性。
      • @Robert scratch 是allDamages 中的一个数组,因此index 是它包含的项目数。您可以运行相同的方法。我刚刚写了allDamages 本身,看看它的哪个孩子的指数最高——原则是一样的。
      【解决方案5】:

      试试:

      var allDamages= {};
      allDamages['scratch'] = [];
      allDamages['scratch'].push({"x":4,"y":6,"index":1});
      allDamages['scratch'].push({"x":3,"y":3,"index":2});
      allDamages['scratch'].push({"x":9,"y":9,"index":3});
      allDamages['scratch'].push({"x":19,"y":39,"index":4});
      allDamages['dent'] = [];
      allDamages['dent'].push({"x":59,"y":69,"index":5});
      allDamages['dent'].push({"x":59,"y":69,"index":9});
      allDamages['dent'].push({"x":39,"y":19,"index":6});
      allDamages['rip'] = [];
      allDamages['rip'].push({"x":20,"y":22,"index":7});
      allDamages['rip'].push({"x":100,"y":56,"index":8});
      var index;
      var cnt = 0;
      var val;
      $.each(allDamages,function(k,v){        
          if(cnt == 0){
              index = highest(v);  //get highest value from each object of allDamages    
              val = k;
          }
          else{
              if(highest(v) > index){
                  index = highest(v);
                  val = k;
              }
          }
          cnt++;    
      });
      console.log("highest : "+index+": "+val);
      var len = allDamages[val].length;
      for(var i=0;i<len;i++){    
          if(allDamages[val][i].index == index){
              allDamages[val].splice(i,1); //remove object having highest value
              break;
          }
      }
      console.log(allDamages);
      function highest(ary) {
          var high = ary[0].index;
          var len = ary.length;
          if(len > 0){
              for(var i=0;i<len;i++){
                  if(ary[i].index > high){
                      high = ary[i].index;
                  }
              }
          }
          return high;
      }
      

      DEMO here.

      【讨论】:

        【解决方案6】:

        我已将数组简化为:

        allDamages.push({"x":39,"y":19,"index":6,"type":'dent'});
        

        这样我就可以正常使用 .pop() 函数了。

        感谢大家的快速回复!!!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多