[英]how to get value on array javascript based index
how to get value on array javascript based index? 如何在基于数组的javascript索引上获取价值?
example : 例如:
var my_array = [1,2,3,4,5,6,7,8,9,10];
how to implements logic like below : 如何实现如下逻辑:
[offset...length]
[2..3] ==> result [3,4,5]
[3..7] ==> result [4,5,6,7,8,9,10]
how do that on javascript? 在javascript上怎么做?
Certainly Mr.fuyushimoya is right - You can use the : JavaScript Array slice() Method 当然Mr.fuyushimoya是正确的-您可以使用:JavaScript Array slice()方法
Definition and Usage:- 定义和用法:
The slice() method returns the selected elements in an array, as a new array object. slice()方法将数组中选定的元素作为新的数组对象返回。
The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument. slice()方法选择从给定start参数开始的元素,并以给定end参数结束但不包括给定end参数的元素。
Note: The original array will not be changed. 注意:原始阵列将不会更改。
For more information , please refer this link. 有关更多信息,请参考此链接。
Example: 例:
var origin = [1,2,3,4,5,6,7,8,9,10]; var start = 3, end = 7, offset = 7; // If you want values from index 3 to index 7 console.log(origin.slice(start, end + 1)); // If you want values from index 3 and the following 6 values. console.log(origin.slice(start, start + offset + 1));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.