【发布时间】: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