1

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

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

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

问候,

罗伯特

4

6 回答 6

0

尝试:

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

演示在这里。

于 2013-12-03T16:33:59.547 回答
0

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
于 2013-12-03T11:51:44.953 回答
0

首先,存储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;
  }
};
于 2013-12-03T12:12:43.727 回答
0

我已将数组简化为:

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

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

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

于 2013-12-04T12:54:37.920 回答
0

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

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));
于 2013-12-03T11:58:39.953 回答
0

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

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

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

于 2013-12-03T12:02:11.110 回答