我有一个很好的谜语,我希望看到解决。 可能有一种更好的方法可以做到这一点,我对想法持开放态度。 我正在尝试为画布绘图应用程序编写撤消功能。 我有以下对象,其中包含具有三个属性的自己的对象的数组。
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'的最高值的条目,然后将其从数组中删除。 这样做的最佳方式是什么?
问候,
罗伯特
I have a nice riddle that I would like to see solved. There might be a better way of doing this and i am open for idea's. I am trying to write an undo function for a canvas drawing app. I have the following object, within it an array with their own objects with three properties.
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});I want to remove the last entry from this array. I want to do this by the property 'index'. So I need to somehow find the entry which has the highest value of the property 'index' and then remove it from the array. What is the best way in doing this?
Greetings,
Robert
最满意答案
allDamages.scratch.length -1返回该数组的最后一个索引。
编辑:
allDamages.scratch.slice(-1).pop()返回最后一个数组项。
如果你只想删除数组中的最后一项,你应该(像Givi所说)在排序数组上使用pop()方法,如下所示:
allDamages['scratch'].pop()
EDIT2:
因为这个问题对我来说并不清楚。 这是我对这个问题的最后一击。
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 indexallDamages.scratch.length -1 returns the last index for that array.
Edit:
allDamages.scratch.slice(-1).pop() returns the last array item.
And if you just want to remove the last item in your array you should (like Givi said) use the pop() method on a sorted array like so:
allDamages['scratch'].pop()
Edit2:
Because the question wasn't clear for me. This is my final shot at the problem.
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更多推荐
发布评论