Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery/Javascript从具有最高属性值的数组中删除项_Javascript_Jquery_Arrays_Properties_Highest - Fatal编程技术网

Jquery/Javascript从具有最高属性值的数组中删除项

Jquery/Javascript从具有最高属性值的数组中删除项,javascript,jquery,arrays,properties,highest,Javascript,Jquery,Arrays,Properties,Highest,我有一个很好的谜语,我想看到它被解开。也许有更好的方法可以做到这一点,我对idea持开放态度。 我正在尝试为画布绘图应用程序编写一个撤销函数。 我有下面的对象,其中有一个数组,它有自己的对象,有三个属性 var allDamages= {}; allDamages['scratch'] = []; allDamages['scratch'].push({"x":4,"y":6,"index":1}); allDamages['scratch'].push({"x":3,"y":3,"index"

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

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”值最高的条目,然后将其从数组中删除。这样做的最佳方式是什么

您好


Robert

返回该数组的最后一个索引

编辑:
alldammars.scratch.slice(-1).pop()
返回最后一个数组项

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

所有损坏['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

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

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

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

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;
}
var undo=函数(输入){
var max=0;
var类型=”;
var指数=0;
for(输入中的变量类型){
//类型:字符串
变量位置=输入[类型];
//位置:阵列
//查找heighest索引属性的位置。
对于(变量i=0;imax){
最大值=位置[i][“索引”];
类型=类型;
指数=指数;
}
}
}
var输出=输入[type]。拼接(索引,1);
返回输出;
}

这将从你的伤害数组中移除具有最大“索引”属性的元素。

首先,为在对象中找到的最高
索引
属性存储一个计数器,并在
刮擦
数组中存储该对象的索引

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;
  }
};
for(var indexCounter=0,indexCounterhighestIndex){
highestIndex=所有损坏。划痕[indexCounter]。索引;
indexInArray=indexCounter;
}
};
试试:

var-allDamages={};
所有损坏['scratch']=[];
所有损坏['scratch'].push({“x”:4,“y”:6,“索引”:1});
所有损坏['scratch'].push({“x”:3,“y”:3,“索引”:2});
所有损坏['scratch'].push({“x”:9,“y”:9,“索引”:3});
所有损坏['scratch'].push({“x”:19,“y”:39,“索引”:4});
所有损坏['dent']=[];
所有损坏['dent'].推动({“x”:59,“y”:69,“索引”:5});
所有损坏['dent'].推动({“x”:59,“y”:69,“索引”:9});
所有损坏['dent'].推动({“x”:39,“y”:19,“索引”:6});
所有损害赔偿['rip']=[];
所有损坏['rip'].推送({“x”:20,“y”:22,“索引”:7});
所有损坏['rip'].推送({“x”:100,“y”:56,“索引”:8});
var指数;
var-cnt=0;
var-val;
$.each(所有损坏,函数(k,v){
如果(cnt==0){
index=highest(v);//从所有损坏的每个对象中获取最高值
val=k;
}
否则{
if(最高(v)>指数){
指数=最高(v);
val=k;
}
}
cnt++;
});
log(“最高:“+index+”:“+val”);
var len=所有损坏[val]。长度;
对于(变量i=0;i 0){
对于(变量i=0;i高){
高=ary[i]。指数;
}
}
}
高回报;
}

我已将阵列简化为:

allDamages.push({"x":39,"y":19,"index":6,"type":'dent'});
这样我就可以正常使用.pop()函数了


谢谢大家的快速回复

在本例中,假设“索引”正在递增,如example@douwe如果他正在进行撤销功能,那么我猜他会删除最后一个索引。我会写另一个解决方案,完全涵盖它。感谢您的反馈;)是的,我认为索引指的是对象文本的索引属性,而不是数组索引。是的,就是属性“index”的属性值,除了你只看“scratch”。如果数组按索引排序,则需要对所有(划痕、凹痕、裂纹、撕裂等)进行比较,您可以使用
alldampairs['scratch'].pop()如果不是,您应该迭代数组get元素以获得最高索引,然后使用
alldampairs['scratch'].splice(indexOfElement,1)看,我得到的答案很接近,但不要给我正确的结果。我只想删除一个条目,而不是删除其域中具有最高索引属性的所有条目。因此,我需要将最高索引[“scratch”]与[“dent”]进行比较,然后决定需要删除哪个条目。感谢您的回复。这很接近。但是您不知道数组“scratch”是否具有整个对象“alldampairs”中最高的“index”属性。@Robert
scratch
alldampairs
中的数组,因此它的
index
是它包含的项数。您可以运行相同的方法。我只是在
AllDemages
上写了一下,看看哪个孩子的指数最高——原则是一样的。
allDamages.push({"x":39,"y":19,"index":6,"type":'dent'});