Q
选择在阵列
-2
A
回答
-1
在你例如,“3”位于索引二的插槽中。如果你想在第三个元素(索引二)之后的第一个函数会做到这一点。
如果你想在找到第一个3之后找到所有的东西,那么第二个函数会这样做。
// This finds all content after index 2
Array.prototype.getEverythingAfterIndexTwo = function() {
if (this.length < 4) {
return [];
} else {
return this.slice(3);
}
}
// This finds the first 3 in the array and returns any content in later indices
Array.prototype.getEverythingAfterAThree = function() {
// returns array if empty
if (!this.length) return this;
// get the index of the first 3 in the array
var threeIndex = this.indexOf(3);
// if no 3 is found or 3 is the last element, returns empty array
// otherwise it returns a new array with the desired content
if (!~threeIndex || threeIndex === this.length-1) {
return [];
} else {
return this.slice(threeIndex + 1);
}
}
var arr=[1,2,3,4,5,6,7,8,9,10];
console.log(arr.getEverythingAfterIndexTwo());
console.log(arr.getEverythingAfterAThree());
1
-1
您拼接功能:
var a = [1,2,3,4,5,6,7,8,9,10];
var b = a.splice(3, a.length);
alert (b); // [4, 5, 6, 7, 8, 9, 10]
alert (a); // [1, 2, 3]
+0
请务必澄清,这会修改原始集合。 – TylerY86
+0
发表了评论,谢谢 – Marcin
+0
@MarcinC。在我的情况下,用户输入数组。所以如果我不知道什么时候是3,我该怎么办? – eclipseIzHere
相关问题
- 1. 选择在阵列
- 2. 选择选项在阵列
- 3. 阵列选择
- 4. 选择阵列
- 5. 发送选择在阵列
- 6. 在选择循环阵列
- 7. 在PHP MYSQL阵列选择
- 8. 在阵列中选择值
- 9. 选择从阵列
- 10. 选择从阵列
- 11. 选择串/阵列
- 12. 选择阵列值
- 13. PHP阵列选择
- 14. 角选择阵列
- 15. HDFStore:选择是否列在阵列
- 16. 选择从阵列蟒蛇
- 17. JQ:选择不从阵列
- 18. PostgreSQL的:选择阵列
- 19. 选择记录到阵列
- 20. 选择匹配阵列总
- 21. 选择排序阵列
- 22. jquery获得选择阵列
- 23. MySQL的选择阵列
- 24. 通过阵列选择
- 25. PHP选择n个阵列
- 26. LINQ选择到阵列
- 27. 阵列中选择查询
- 28. 随机选择矩阵列
- 29. 追加JSON阵列选择
- 30. 选择矩阵列名的
你可以像'arr.slice(arr.indexOf(3)+1)' – Redu
@Redu我甚至扩展这个为'arr.slice(arr.indexOf( 3)+1 || arr.length)'如果'arr'不包含3,则返回一个空的Array。虽然没有明确要求。 – Thomas
我必须投票,“不显示任何研究工作”。 – TylerY86