如何获得基于数组javascript的索引的价值

时间:2023-01-26 14:47:51

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?

1 个解决方案

#1


0  

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

#1


0  

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