简体   繁体   中英

how to get value on array javascript based index

how to get value on array javascript based index?

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?

Certainly Mr.fuyushimoya is right - You can use the : JavaScript Array slice() Method


Definition and Usage:-
The slice() method returns the selected elements in an array, as a new array object.

The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument.

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)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM