我有一个很好的谜语,我希望看到解决。可能有一种更好的方法可以做到这一点,我对这个想法持开放态度。 我正在尝试为画布绘图应用程序编写撤消功能。 我有以下对象,其中包含一个具有三个属性的对象的数组。
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'来做到这一点。 所以我需要以某种方式找到具有属性'index'的最高值的条目,然后将其从数组中删除。这样做的最佳方式是什么?
问候,
罗伯特
答案 0 :(得分:0)
allDamages.scratch.length -1
返回该数组的最后一个索引。
allDamages.scratch.slice(-1).pop()
返回最后一个数组项。
如果您只想删除数组中的最后一项,您应该(如Givi所说)在排序数组上使用pop()
方法,如下所示:
allDamages['scratch'].pop()
因为这个问题对我来说并不清楚。这是我对这个问题的最后一击。
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
答案 1 :(得分: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));
答案 2 :(得分: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;
}
这应该删除具有最大&#34;索引的元素&#34;来自你的伤害阵列的财产。
答案 3 :(得分: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;
}
};
答案 4 :(得分: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;
}
答案 5 :(得分:0)
我已将我的数组简化为:
allDamages.push({"x":39,"y":19,"index":6,"type":'dent'});
这样我可以正常使用.pop()函数。
谢谢大家的快速回复!