我有一个对象数组。每个对象都有一个ID属性。我想在具有特定ID的对象的数组中找到索引。有没有在jQuery中做到这一点的优雅和简单的方法?jQuery:数组中元素的索引,其中谓词
回答
在这种情况下,您应该在javascript中使用for
循环,而不是使用jQuery。见路3 http://net.tutsplus.com/tutorials/javascript-ajax/10-ways-to-instantly-increase-your-jquery-performance/
更新: jQuery是用JavaScript编写的,它不能比也用JavaScript编写的另一个代码更快。如果你使用DOM,jQuery是非常好的,但如果你使用简单的javascript数组或对象工作,那么jQuery就没有什么帮助。
你正在寻找的代码可以是这样的:
for (var i=0, l = ar.length; i<l; i++) {
if (ar[i].ID === specificID) {
// i is the index. You can use it here directly or make a break
// and use i after the loop (variables in javascript declared
// in a block can be used anywhere in the same function)
break;
}
}
if (i<l) {
// i is the index
}
重要的是你应该持有一些简单的JavaScript规则:始终声明局部变量(不要忘了var
变量声明之前)和缓存您在本地变量中使用多次的任何属性或索引(如上面的ar.length
)。 (参见例如http://wiki.forum.nokia.com/index.php/JavaScript_Performance_Best_Practices)
不是真的优雅,但一个可爱的把戏:
var index = parseInt(
$.map(array, function(i, o) { return o.id === target ? i : ''; }).join('')
);
的jQuery没有很多这样的功能性结构的;图书馆的哲学着重于DOM争夺的工作。他们甚至不会添加一个.reduce()
函数,因为没有人能想到它对核心功能有用的原因。
Underscore.js库有很多这样的工具,它用jQuery“很好玩”。
See [`Array.filter`][1] to filter an array with a callback function. Each object in the array will be passed to the callback function one by one. The callback function must return `true` if the value is to be included, or false if not. var matchingIDs = objects.filter(function(o) { return o.ID == searchTerm; }); All objects having the ID as searchTerm will be returned as an array to matchingIDs. Get the matching element from the first index (assuming ID is unique and there's only gonna be one) matchingIDs[0]; [1]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/filter
更新:
结帐findIndex
从ECMAScript中6
items.findIndex(function(item) { item.property == valueToSearch; });
由于findIndex
不适用于大多数浏览器还没有,你可以回填它使用这implementation:
if (!Array.prototype.findIndex) {
Array.prototype.findIndex = function(predicate) {
if (this == null) {
throw new TypeError('Array.prototype.findIndex called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
for (var i = 0; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return i;
}
}
return -1;
};
}
使用j订购。 http://github.com/danstocker/jorder
将您的数组放入jOrder表中,并在'ID'字段中添加一个索引。
var table = jOrder(data)
.index('id', ['ID']);
然后,通过获取元素的数组索引:
var arrayidx = table.index('id').lookup([{ ID: MyID }]);
如果你想整行,则:
var filtered = table.where([{ ID: MyID }]);
瞧。
有没有内置的方法,该[].indexOf()
方法不采取谓词,因此你需要一些定制:
function indexOf(array, predicate)
{
for (var i = 0, n = array.length; i != n; ++i) {
if (predicate(array[i])) {
return i;
}
}
return -1;
}
var index = indexOf(arr, function(item) {
return item.ID == 'foo';
});
函数返回-1如果断言永远不会产生一个truthy值。
- 1. Julia:用谓词索引多维数组
- 2. 的XPath谓词组合多个元素
- 3. 使用谓词条件搜索元素
- 4. Numpy数组中的元素索引1
- 5. 装配中的索引数组元素
- 6. shell脚本中的数组的最大元素及其索引
- 7. 获取元素的索引数组包含其他元素
- 8. 使用Javascript/jQuery的从元素位/索引获取的数组元素索引
- 9. 在选择器数组中的元素的jQuery索引
- 10. Python数组 - 索引元素
- 11. 点击元素索引从数组中弹出她的event_id jquery
- 12. jQuery元素索引
- 13. 引用数组元素(jQuery)
- 14. numpy binning:如何获得满足谓词的数组索引
- 15. 优化索引和检索Python中numpy数组中的元素?
- 16. 数组索引与数组元素
- 17. MongoDB数组谓词
- 18. 使用键删除索引数组中的数组元素
- 19. 如何在数组数组中找到元素的索引?
- 20. Jquery - 通过其索引访问类中的输入元素
- 21. 使用Regex将数组索引替换为数组元素。 Jquery
- 22. 如何调用数组的元素作为其索引数值
- 23. Marklogic:元素范围索引VS元素词词典
- 24. PHP - 从数组中的某个索引中获取元素,其中的某个元素具有一定的值
- 25. 如何筛选列表中谓词遍及其他列表中的元素
- 26. jQuery中获取使用索引()的元素的索引
- 27. 在JS/JQuery中查找由toArray创建的数组中的元素的索引
- 28. 如何在一个数组中搜索奇数索引元素
- 29. 如何增加MongoDB中由索引引用的数组元素?
- 30. xquery返回两个谓词的元素?
准确地说,我也在寻找。太糟糕了没有优雅的解决方案确实... – epeleg 2012-07-18 20:45:34