如何使用 jquery 从数组中获取特定元素?假设我有一个数组
var arr = [Obj1, Obj2, Obj3, Obj4, Obj5, Obj6]
我得到了我需要选择的对象的索引,比如 3 和 5。有没有一种有效的方法来做到这一点?就像是
var result = arr.someFunction([3, 5])
You could well add a prototype method to do this :) example:
#this adds the a new method called "getElementsAt" to the array object
Array.prototype.getElementsAt=function(elements)
{
var outputarray = [];
for (var i = 0; i < elements.length; i++) {
outputarray[i] = this[elements[i]];
}
return outputarray;
}
#then do your thing
var arr = ["item0","item1","item2","item3","item4","item5","item6"]
var result = arr.getElementsAt([3, 5])